PostgreSQL Source Code: src/include/utils/memutils.h Source File (original) (raw)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17#ifndef MEMUTILS_H

18#define MEMUTILS_H

19

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40#define MaxAllocSize ((Size) 0x3fffffff)

41

42#define AllocSizeIsValid(size) ((Size) (size) <= MaxAllocSize)

43

44

45#define MaxAllocHugeSize (SIZE_MAX / 2)

46

47#define InvalidAllocSize SIZE_MAX

48

49#define AllocHugeSizeIsValid(size) ((Size) (size) <= MaxAllocHugeSize)

50

51

52

53

54

55

56

57

65

66

68

69

70

71

72

91 int max_level, int max_children,

92 bool print_to_stderr);

94 bool allow);

95

96#ifdef MEMORY_CONTEXT_CHECKING

97extern void MemoryContextCheck(MemoryContext context);

98#endif

99

100

101#define MemoryContextCopyAndSetIdentifier(cxt, id) \

102 MemoryContextSetIdentifier(cxt, MemoryContextStrdup(cxt, id))

103

106

107

108

109

110

111

113 const char *name,

114 Size minContextSize,

115 Size initBlockSize,

116 Size maxBlockSize);

117

118

119

120

121

122

123#ifdef HAVE__BUILTIN_CONSTANT_P

124#define AllocSetContextCreate(parent, name, ...) \

125 (StaticAssertExpr(__builtin_constant_p(name), \

126 "memory context names must be constant strings"), \

127 AllocSetContextCreateInternal(parent, name, __VA_ARGS__))

128#else

129#define AllocSetContextCreate \

130 AllocSetContextCreateInternal

131#endif

132

133

135 const char *name,

136 Size blockSize,

137 Size chunkSize);

138

139

141 const char *name,

142 Size minContextSize,

143 Size initBlockSize,

144 Size maxBlockSize);

145

146

148 const char *name,

149 Size minContextSize,

150 Size initBlockSize,

151 Size maxBlockSize);

152

153

154

155

156

157#define ALLOCSET_DEFAULT_MINSIZE 0

158#define ALLOCSET_DEFAULT_INITSIZE (8 * 1024)

159#define ALLOCSET_DEFAULT_MAXSIZE (8 * 1024 * 1024)

160#define ALLOCSET_DEFAULT_SIZES \

161 ALLOCSET_DEFAULT_MINSIZE, ALLOCSET_DEFAULT_INITSIZE, ALLOCSET_DEFAULT_MAXSIZE

162

163

164

165

166

167#define ALLOCSET_SMALL_MINSIZE 0

168#define ALLOCSET_SMALL_INITSIZE (1 * 1024)

169#define ALLOCSET_SMALL_MAXSIZE (8 * 1024)

170#define ALLOCSET_SMALL_SIZES \

171 ALLOCSET_SMALL_MINSIZE, ALLOCSET_SMALL_INITSIZE, ALLOCSET_SMALL_MAXSIZE

172

173

174

175

176

177#define ALLOCSET_START_SMALL_SIZES \

178 ALLOCSET_SMALL_MINSIZE, ALLOCSET_SMALL_INITSIZE, ALLOCSET_DEFAULT_MAXSIZE

179

180

181

182

183

184

185

186

187#define ALLOCSET_SEPARATE_THRESHOLD 8192

188

189#define SLAB_DEFAULT_BLOCK_SIZE (8 * 1024)

190#define SLAB_LARGE_BLOCK_SIZE (8 * 1024 * 1024)

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218static inline bool

220{

221 const unsigned char *p = (const unsigned char *) ptr;

222 const unsigned char *end = &p[len];

223 const unsigned char *aligned_end = (const unsigned char *)

224 ((uintptr_t) end & (~(sizeof(size_t) - 1)));

225

226 if (len < sizeof(size_t))

227 {

228 while (p < end)

229 {

230 if (*p++ != 0)

231 return false;

232 }

233 return true;

234 }

235

236

237 if (len < sizeof(size_t) * 8)

238 {

239

240 while (((uintptr_t) p & (sizeof(size_t) - 1)) != 0)

241 {

242 if (p == end)

243 return true;

244 if (*p++ != 0)

245 return false;

246 }

247

248

249

250

251

252

253

254 for (; p < aligned_end; p += sizeof(size_t))

255 {

256 if (*(size_t *) p != 0)

257 return false;

258 }

259

260

261 while (p < end)

262 {

263 if (*p++ != 0)

264 return false;

265 }

266 return true;

267 }

268

269

270

271

272 while (((uintptr_t) p & (sizeof(size_t) - 1)) != 0)

273 {

274 if (p == end)

275 return true;

276

277 if (*p++ != 0)

278 return false;

279 }

280

281

282

283

284

285

286

287

288

289

290

291 for (; p < aligned_end - (sizeof(size_t) * 7); p += sizeof(size_t) * 8)

292 {

293 if ((((size_t *) p)[0] != 0) | (((size_t *) p)[1] != 0) |

294 (((size_t *) p)[2] != 0) | (((size_t *) p)[3] != 0) |

295 (((size_t *) p)[4] != 0) | (((size_t *) p)[5] != 0) |

296 (((size_t *) p)[6] != 0) | (((size_t *) p)[7] != 0))

297 return false;

298 }

299

300

301

302

303

304

305

306 for (; p < aligned_end; p += sizeof(size_t))

307 {

308 if (*(size_t *) p != 0)

309 return false;

310 }

311

312

313 while (p < end)

314 {

315 if (*p++ != 0)

316 return false;

317 }

318

319 return true;

320}

321

322#endif

PGDLLIMPORT MemoryContext CurTransactionContext

bool MemoryContextIsEmpty(MemoryContext context)

void MemoryContextMemConsumed(MemoryContext context, MemoryContextCounters *consumed)

void MemoryContextReset(MemoryContext context)

PGDLLIMPORT MemoryContext MessageContext

PGDLLIMPORT MemoryContext TopMemoryContext

PGDLLIMPORT MemoryContext TopTransactionContext

void HandleLogMemoryContextInterrupt(void)

void MemoryContextSetParent(MemoryContext context, MemoryContext new_parent)

Size GetMemoryChunkSpace(void *pointer)

void MemoryContextDeleteChildren(MemoryContext context)

MemoryContext GetMemoryChunkContext(void *pointer)

PGDLLIMPORT MemoryContext PostmasterContext

void MemoryContextStatsDetail(MemoryContext context, int max_level, int max_children, bool print_to_stderr)

Size MemoryContextMemAllocated(MemoryContext context, bool recurse)

void MemoryContextStats(MemoryContext context)

MemoryContext SlabContextCreate(MemoryContext parent, const char *name, Size blockSize, Size chunkSize)

void MemoryContextInit(void)

MemoryContext BumpContextCreate(MemoryContext parent, const char *name, Size minContextSize, Size initBlockSize, Size maxBlockSize)

PGDLLIMPORT MemoryContext CacheMemoryContext

MemoryContext MemoryContextGetParent(MemoryContext context)

void ProcessLogMemoryContextInterrupt(void)

PGDLLIMPORT MemoryContext PortalContext

MemoryContext GenerationContextCreate(MemoryContext parent, const char *name, Size minContextSize, Size initBlockSize, Size maxBlockSize)

static bool pg_memory_is_all_zeros(const void *ptr, size_t len)

void MemoryContextDelete(MemoryContext context)

void MemoryContextAllowInCriticalSection(MemoryContext context, bool allow)

void MemoryContextResetChildren(MemoryContext context)

void MemoryContextSetIdentifier(MemoryContext context, const char *id)

void MemoryContextResetOnly(MemoryContext context)

PGDLLIMPORT MemoryContext ErrorContext

MemoryContext AllocSetContextCreateInternal(MemoryContext parent, const char *name, Size minContextSize, Size initBlockSize, Size maxBlockSize)