PostgreSQL Source Code: src/backend/utils/mmgr/mcxt.c Source File (original) (raw)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

40

47

48

49static void BogusFree(void *pointer);

50static void *BogusRealloc(void *pointer, Size size, int flags);

53

54

55

56

57#define BOGUS_MCTX(id) \

58 [id].free_p = BogusFree, \

59 [id].realloc = BogusRealloc, \

60 [id].get_chunk_context = BogusGetChunkContext, \

61 [id].get_chunk_space = BogusGetChunkSpace

62

64

74#ifdef MEMORY_CONTEXT_CHECKING

76#endif

77

78

88#ifdef MEMORY_CONTEXT_CHECKING

90#endif

91

92

102#ifdef MEMORY_CONTEXT_CHECKING

104#endif

105

106

116#ifdef MEMORY_CONTEXT_CHECKING

118#endif

119

120

130#ifdef MEMORY_CONTEXT_CHECKING

132#endif

133

134

135

136

137

138

139

140

152};

153

154#undef BOGUS_MCTX

155

156

157

158

159

161

162

163

164

165

173

174

176

177

179

189

190

191

192

193

194

195#define AssertNotInCriticalSection(context) \

196 Assert(CritSectionCount == 0 || (context)->allowInCritSection)

197

198

199

200

201

202#define MCXT_METHOD(pointer, method) \

203 mcxt_methods[GetMemoryChunkMethodID(pointer)].method

204

205

206

207

208

209

212{

214

215

216

217

218

219

221

222

224

225 header = *((const uint64 *) ((const char *) pointer - sizeof(uint64)));

226

227

229

231}

232

233

234

235

236

237

238

241{

243

244

246

247 header = *((const uint64 *) ((const char *) pointer - sizeof(uint64)));

248

249

251

252 return header;

253}

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

278{

279

280 if (curr->firstchild != NULL)

282

283

284

285

286

287

288

289

290 while (curr->nextchild == NULL)

291 {

295 }

297}

298

299

300

301

302

303

304

305static void

307{

308 elog(ERROR, "pfree called with invalid pointer %p (header 0x%016" PRIx64 ")",

310}

311

312static void *

314{

315 elog(ERROR, "repalloc called with invalid pointer %p (header 0x%016" PRIx64 ")",

317 return NULL;

318}

319

322{

323 elog(ERROR, "GetMemoryChunkContext called with invalid pointer %p (header 0x%016" PRIx64 ")",

325 return NULL;

326}

327

330{

331 elog(ERROR, "GetMemoryChunkSpace called with invalid pointer %p (header 0x%016" PRIx64 ")",

333 return 0;

334}

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358void

360{

362

363

364

365

367 "TopMemoryContext",

369

370

371

372

373

375

376

377

378

379

380

381

382

383

384

385

386

387

388

390 "ErrorContext",

391 8 * 1024,

392 8 * 1024,

393 8 * 1024);

395}

396

397

398

399

400

401

402void

404{

406

407

410

411

414}

415

416

417

418

419

420

421void

423{

425

426

428 {

430

431

432

433

434

435

436

437

438

441 }

442}

443

444

445

446

447

448

449

450void

452{

454

458 {

460 }

461}

462

463

464

465

466

467

468

469

470

471void

473{

475

477

478

479

480

481

482

483

484

485

486 curr = context;

487 for (;;)

488 {

490

491

492 while (curr->firstchild != NULL)

494

495

496

497

498

501

502 if (curr == context)

503 break;

504 curr = parent;

505 }

506}

507

508

509

510

511

512

513static void

515{

517

519

521

523

524

525

526

527

528

529

531

532

533

534

535

536

538

539

540

541

542

543

545

547}

548

549

550

551

552

553

554void

556{

558

559

560

561

562

565}

566

567

568

569

570

571

572

573

574

575

576

577

578

579

580

581void

584{

586

587

590

591 context->isReset = false;

592}

593

594

595

596

597

598

599

600

601

602

603

604

605

606void

609{

612

614

617 {

618 if (cur != cb)

619 continue;

620 if (prev)

622 else

624 return;

625 }

627}

628

629

630

631

632

633static void

635{

637

638

639

640

641

642

644 {

647 }

648}

649

650

651

652

653

654

655

656

657

658

659

660void

662{

664 context->ident = id;

665}

666

667

668

669

670

671

672

673

674

675

676

677

678

679

680

681

682

683

684

685void

687{

690

691

693 return;

694

695

697 {

699

702 else

703 {

706 }

707

710 }

711

712

714 {

722 }

723 else

724 {

728 }

729}

730

731

732

733

734

735

736

737

738

739

740

741

742void

749

750

751

752

753

754

757{

758 return MCXT_METHOD(pointer, get_chunk_context) (pointer);

759}

760

761

762

763

764

765

766

767

768

771{

772 return MCXT_METHOD(pointer, get_chunk_space) (pointer);

773}

774

775

776

777

778

781{

783

784 return context->parent;

785}

786

787

788

789

790

791bool

793{

795

796

797

798

799

801 return false;

802

804}

805

806

807

808

809

812{

814

816

817 if (recurse)

818 {

822 {

823 total += curr->mem_allocated;

824 }

825 }

826

827 return total;

828}

829

830

831

832

833

834void

837{

839

841

842

844

845

849 {

851 }

852}

853

854

855

856

857

858

859

860

861

862void

868

869

870

871

872

873

874

875

876

877void

881{

883

885

888

891 "Grand total: %zu bytes in %zu blocks; %zu free (%zu chunks); %zu used\n",

895 else

896 {

897

898

899

900

901

902

903

904

905

906

910 errmsg_internal("Grand total: %zu bytes in %zu blocks; %zu free (%zu chunks); %zu used",

914 }

915}

916

917

918

919

920

921

922

923

924static void

929{

932

934

935

938 &level,

940

941

942

943

944

945

946

947

948

952 {

955 {

960 }

961 }

962

963 if (child != NULL)

964 {

965

967

969

971 while (child != NULL)

972 {

976 }

977

979 {

980 for (int i = 0; i < level; i++)

983 "%d more child contexts containing %zu total in %zu blocks; %zu free (%zu chunks); %zu used\n",

990 }

991 else

995 errmsg_internal("level: %d; %d more child contexts containing %zu total in %zu blocks; %zu free (%zu chunks); %zu used",

996 level,

1003

1005 {

1010 }

1011 }

1012}

1013

1014

1015

1016

1017

1018

1019

1020

1021static void

1025{

1026 int level = *(int *) passthru;

1027 const char *name = context->name;

1030 int i;

1031

1032

1033

1034

1035

1036

1038 {

1041 }

1042

1044

1046 {

1047

1048

1049

1050

1051

1052

1054 bool truncated = false;

1055

1058

1059 if (idlen > 100)

1060 {

1062 truncated = true;

1063 }

1064

1065 while (idlen-- > 0)

1066 {

1067 unsigned char c = *ident++;

1068

1069 if (c < ' ')

1070 c = ' ';

1072 }

1074

1075 if (truncated)

1077 }

1078

1080 {

1081 for (i = 1; i < level; i++)

1084 }

1085 else

1091}

1092

1093

1094

1095

1096

1097

1098

1099#ifdef MEMORY_CONTEXT_CHECKING

1100void

1102{

1104 context->methods->check(context);

1105

1109 {

1112 }

1113}

1114#endif

1115

1116

1117

1118

1119

1120

1121

1122

1123

1124

1125

1126

1127

1128

1129

1130

1131

1132

1133

1134

1135

1136

1137

1138

1139

1140

1141

1142

1143

1144

1145

1146

1147

1148void

1153 const char *name)

1154{

1155

1157

1158

1160 Assert(node != parent);

1161

1162

1163 node->type = tag;

1166 node->parent = parent;

1173

1174

1175 if (parent)

1176 {

1181

1183 }

1184 else

1185 {

1188 }

1189}

1190

1191

1192

1193

1194

1195

1196

1197void *

1199{

1201 {

1206 errmsg("out of memory"),

1207 errdetail("Failed on request of size %zu in memory context \"%s\".",

1208 size, context->name)));

1209 }

1210 return NULL;

1211}

1212

1213

1214

1215

1216

1217

1218void

1220{

1221 elog(ERROR, "invalid memory alloc request size %zu", size);

1222}

1223

1224

1225

1226

1227

1228

1229

1230

1231void *

1233{

1234 void *ret;

1235

1238

1239 context->isReset = false;

1240

1241

1242

1243

1244

1245

1246

1247

1248

1249

1250

1251 ret = context->methods->alloc(context, size, 0);

1252

1254

1255 return ret;

1256}

1257

1258

1259

1260

1261

1262

1263

1264

1265void *

1267{

1268 void *ret;

1269

1272

1273 context->isReset = false;

1274

1275 ret = context->methods->alloc(context, size, 0);

1276

1278

1280

1281 return ret;

1282}

1283

1284

1285

1286

1287

1288void *

1290{

1291 void *ret;

1292

1295

1298 elog(ERROR, "invalid memory alloc request size %zu", size);

1299

1300 context->isReset = false;

1301

1302 ret = context->methods->alloc(context, size, flags);

1304 return NULL;

1305

1307

1310

1311 return ret;

1312}

1313

1314

1315

1316

1317

1318

1319

1320

1321

1322void

1329

1330

1331

1332

1333

1334

1335

1336

1337

1338

1339void

1341{

1343

1344

1345

1346

1347

1348

1349

1351 return;

1353

1355 {

1356

1357

1358

1359

1363 errmsg("logging memory contexts of PID %d", MyProcPid)));

1364

1365

1366

1367

1368

1369

1370

1371

1372

1373

1374

1375

1376

1378 }

1380 {

1382 }

1384}

1385

1386void *

1388{

1389

1390 void *ret;

1392

1395

1396 context->isReset = false;

1397

1398

1399

1400

1401

1402

1403

1404

1405

1406

1407

1408 ret = context->methods->alloc(context, size, 0);

1409

1412

1413 return ret;

1414}

1415

1416void *

1418{

1419

1420 void *ret;

1422

1425

1426 context->isReset = false;

1427

1428 ret = context->methods->alloc(context, size, 0);

1429

1432

1434

1435 return ret;

1436}

1437

1438void *

1440{

1441

1442 void *ret;

1444

1447

1448 context->isReset = false;

1449

1450 ret = context->methods->alloc(context, size, flags);

1452 {

1453

1455 return NULL;

1456 }

1457

1459

1462

1463 return ret;

1464}

1465

1466

1467

1468

1469

1470

1471

1472

1473

1474

1475

1476

1477

1478

1479

1480

1481void *

1484{

1489

1490

1491

1492

1493

1494

1495

1496

1498

1499

1501

1502

1503

1504

1505

1508

1509

1510

1511

1512

1513

1514

1515

1516

1517

1518

1519

1520

1521

1522

1523

1524

1525

1527

1528#ifdef MEMORY_CONTEXT_CHECKING

1529

1531#endif

1532

1533

1534

1535

1536

1537

1540

1541

1544

1546

1547

1548

1549

1550

1551

1552

1553

1554

1555

1556

1559

1560

1562

1563#ifdef MEMORY_CONTEXT_CHECKING

1565

1567#endif

1568

1569

1570

1571

1572

1573

1574

1575

1576

1577

1578

1579

1582

1583

1586

1588}

1589

1590

1591

1592

1593

1594

1595

1596

1597

1598

1599

1600

1601

1602

1603

1604

1605void *

1610

1611

1612

1613

1614

1615void

1617{

1618#ifdef USE_VALGRIND

1620#endif

1621

1623

1625}

1626

1627

1628

1629

1630

1631void *

1633{

1634#if defined(USE_ASSERT_CHECKING) || defined(USE_VALGRIND)

1636#endif

1637 void *ret;

1638

1640

1641

1643

1644

1645

1646

1647

1648

1649

1650

1651

1652

1653

1655

1657

1658 return ret;

1659}

1660

1661

1662

1663

1664

1665

1666void *

1668{

1669#if defined(USE_ASSERT_CHECKING) || defined(USE_VALGRIND)

1671#endif

1672 void *ret;

1673

1675

1676

1678

1679

1680

1681

1682

1683

1684

1685

1686

1687

1688

1691 return NULL;

1692

1694

1695 return ret;

1696}

1697

1698

1699

1700

1701

1702

1703void *

1705{

1706 void *ret;

1707

1708

1710 elog(ERROR, "invalid repalloc0 call: oldsize %zu, new size %zu",

1712

1713 ret = repalloc(pointer, size);

1715 return ret;

1716}

1717

1718

1719

1720

1721

1722

1723

1724void *

1726{

1727 void *ret;

1728

1731

1732 context->isReset = false;

1733

1734

1735

1736

1737

1738

1739

1740

1741

1742

1743

1745

1747

1748 return ret;

1749}

1750

1751

1752

1753

1754

1755

1756void *

1762

1763

1764

1765

1766

1767char *

1769{

1772

1774

1776

1777 return nstr;

1778}

1779

1780char *

1785

1786

1787

1788

1789

1790

1791char *

1793{

1794 char *out;

1795

1797

1800 out[len] = '\0';

1801

1802 return out;

1803}

1804

1805

1806

1807

1808char *

1810{

1811 size_t n;

1812

1814 while (n > 0 && in[n - 1] == '\n')

1815 n--;

1817}

MemoryContext AlignedAllocGetChunkContext(void *pointer)

void * AlignedAllocRealloc(void *pointer, Size size, int flags)

Size AlignedAllocGetChunkSpace(void *pointer)

void AlignedAllocFree(void *pointer)

void AllocSetReset(MemoryContext context)

void * AllocSetRealloc(void *pointer, Size size, int flags)

Size AllocSetGetChunkSpace(void *pointer)

MemoryContext AllocSetGetChunkContext(void *pointer)

void AllocSetStats(MemoryContext context, MemoryStatsPrintFunc printfunc, void *passthru, MemoryContextCounters *totals, bool print_to_stderr)

bool AllocSetIsEmpty(MemoryContext context)

void * AllocSetAlloc(MemoryContext context, Size size, int flags)

void AllocSetFree(void *pointer)

void AllocSetDelete(MemoryContext context)

void BumpFree(void *pointer)

void BumpDelete(MemoryContext context)

Size BumpGetChunkSpace(void *pointer)

void BumpStats(MemoryContext context, MemoryStatsPrintFunc printfunc, void *passthru, MemoryContextCounters *totals, bool print_to_stderr)

MemoryContext BumpGetChunkContext(void *pointer)

void BumpReset(MemoryContext context)

bool BumpIsEmpty(MemoryContext context)

void * BumpRealloc(void *pointer, Size size, int flags)

void * BumpAlloc(MemoryContext context, Size size, int flags)

#define TYPEALIGN(ALIGNVAL, LEN)

#define Assert(condition)

#define MemSetAligned(start, val, len)

#define fprintf(file, fmt, msg)

int errcode(int sqlerrcode)

int int errhidestmt(bool hide_stmt)

int errdetail(const char *fmt,...) pg_attribute_printf(1

int int errmsg_internal(const char *fmt,...) pg_attribute_printf(1

int errhidecontext(bool hide_ctx)

#define ereport(elevel,...)

#define MCXT_ALLOC_NO_OOM

void * GenerationRealloc(void *pointer, Size size, int flags)

void GenerationReset(MemoryContext context)

void GenerationFree(void *pointer)

MemoryContext GenerationGetChunkContext(void *pointer)

Size GenerationGetChunkSpace(void *pointer)

bool GenerationIsEmpty(MemoryContext context)

void GenerationStats(MemoryContext context, MemoryStatsPrintFunc printfunc, void *passthru, MemoryContextCounters *totals, bool print_to_stderr)

void GenerationDelete(MemoryContext context)

void * GenerationAlloc(MemoryContext context, Size size, int flags)

volatile sig_atomic_t LogMemoryContextPending

volatile sig_atomic_t InterruptPending

volatile uint32 CritSectionCount

int pg_mbcliplen(const char *mbstr, int len, int limit)

static void MemoryContextStatsInternal(MemoryContext context, int level, int max_level, int max_children, MemoryContextCounters *totals, bool print_to_stderr)

void MemoryContextUnregisterResetCallback(MemoryContext context, MemoryContextCallback *cb)

void * repalloc0(void *pointer, Size oldsize, Size size)

static void MemoryContextCallResetCallbacks(MemoryContext context)

char * MemoryContextStrdup(MemoryContext context, const char *string)

void * MemoryContextAlloc(MemoryContext context, Size size)

MemoryContext MessageContext

bool MemoryContextIsEmpty(MemoryContext context)

void MemoryContextMemConsumed(MemoryContext context, MemoryContextCounters *consumed)

void MemoryContextReset(MemoryContext context)

void MemoryContextCreate(MemoryContext node, NodeTag tag, MemoryContextMethodID method_id, MemoryContext parent, const char *name)

void * MemoryContextAllocZero(MemoryContext context, Size size)

MemoryContext TopTransactionContext

char * pstrdup(const char *in)

void HandleLogMemoryContextInterrupt(void)

void MemoryContextRegisterResetCallback(MemoryContext context, MemoryContextCallback *cb)

static MemoryContextMethodID GetMemoryChunkMethodID(const void *pointer)

void MemoryContextSetParent(MemoryContext context, MemoryContext new_parent)

static void * BogusRealloc(void *pointer, Size size, int flags)

void * repalloc(void *pointer, Size size)

void pfree(void *pointer)

Size GetMemoryChunkSpace(void *pointer)

void * palloc0(Size size)

static Size BogusGetChunkSpace(void *pointer)

void * MemoryContextAllocAligned(MemoryContext context, Size size, Size alignto, int flags)

void MemoryContextDeleteChildren(MemoryContext context)

MemoryContext TopMemoryContext

char * pchomp(const char *in)

#define AssertNotInCriticalSection(context)

MemoryContext CurTransactionContext

MemoryContext CurrentMemoryContext

static MemoryContext MemoryContextTraverseNext(MemoryContext curr, MemoryContext top)

static bool LogMemoryContextInProgress

MemoryContext GetMemoryChunkContext(void *pointer)

void * MemoryContextAllocExtended(MemoryContext context, Size size, int flags)

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

Size MemoryContextMemAllocated(MemoryContext context, bool recurse)

char * pnstrdup(const char *in, Size len)

void MemoryContextStats(MemoryContext context)

void MemoryContextInit(void)

static void BogusFree(void *pointer)

void * palloc_extended(Size size, int flags)

MemoryContext PostmasterContext

void * MemoryContextAllocationFailure(MemoryContext context, Size size, int flags)

static const MemoryContextMethods mcxt_methods[]

void * repalloc_extended(void *pointer, Size size, int flags)

MemoryContext MemoryContextGetParent(MemoryContext context)

void ProcessLogMemoryContextInterrupt(void)

MemoryContext ErrorContext

static MemoryContext BogusGetChunkContext(void *pointer)

MemoryContext CacheMemoryContext

void MemoryContextSizeFailure(MemoryContext context, Size size, int flags)

void * MemoryContextAllocHuge(MemoryContext context, Size size)

void MemoryContextDelete(MemoryContext context)

void MemoryContextAllowInCriticalSection(MemoryContext context, bool allow)

static void MemoryContextDeleteOnly(MemoryContext context)

void MemoryContextResetChildren(MemoryContext context)

static void MemoryContextStatsPrint(MemoryContext context, void *passthru, const char *stats_string, bool print_to_stderr)

void * repalloc_huge(void *pointer, Size size)

void MemoryContextSetIdentifier(MemoryContext context, const char *id)

void MemoryContextResetOnly(MemoryContext context)

static uint64 GetMemoryChunkHeader(const void *pointer)

MemoryContext PortalContext

void * palloc_aligned(Size size, Size alignto, int flags)

#define MCXT_METHOD(pointer, method)

#define VALGRIND_MAKE_MEM_DEFINED(addr, size)

#define VALGRIND_MEMPOOL_CHANGE(context, optr, nptr, size)

#define VALGRIND_MEMPOOL_ALLOC(context, addr, size)

#define VALGRIND_MEMPOOL_FREE(context, addr)

#define VALGRIND_MAKE_MEM_NOACCESS(addr, size)

#define MemoryContextIsValid(context)

#define AllocSetContextCreate

#define ALLOCSET_DEFAULT_SIZES

#define AllocHugeSizeIsValid(size)

#define AllocSizeIsValid(size)

#define MEMORY_CONTEXT_METHODID_MASK

#define PallocAlignedExtraBytes(alignto)

@ MCTX_15_RESERVED_WIPEDMEM_ID

@ MCTX_1_RESERVED_GLIBC_ID

@ MCTX_0_RESERVED_UNUSEDMEM_ID

@ MCTX_2_RESERVED_GLIBC_ID

@ MCTX_ALIGNED_REDIRECT_ID

#define PointerGetMemoryChunk(p)

static void MemoryChunkSetHdrMask(MemoryChunk *chunk, void *block, Size value, MemoryContextMethodID methodid)

void * SlabAlloc(MemoryContext context, Size size, int flags)

void SlabFree(void *pointer)

void SlabReset(MemoryContext context)

Size SlabGetChunkSpace(void *pointer)

bool SlabIsEmpty(MemoryContext context)

MemoryContext SlabGetChunkContext(void *pointer)

void * SlabRealloc(void *pointer, Size size, int flags)

void SlabStats(MemoryContext context, MemoryStatsPrintFunc printfunc, void *passthru, MemoryContextCounters *totals, bool print_to_stderr)

void SlabDelete(MemoryContext context)

bool stack_is_too_deep(void)

struct MemoryContextCallback * next

MemoryContextCallbackFunction func

MemoryContextCallback * reset_cbs

const MemoryContextMethods * methods

void(* delete_context)(MemoryContext context)

void(* stats)(MemoryContext context, MemoryStatsPrintFunc printfunc, void *passthru, MemoryContextCounters *totals, bool print_to_stderr)

bool(* is_empty)(MemoryContext context)

void *(* alloc)(MemoryContext context, Size size, int flags)

void(* reset)(MemoryContext context)