Hướng dẫn what is static memory in python? - bộ nhớ tĩnh trong python là gì?

Tổng quan¶

Quản lý bộ nhớ trong Python liên quan đến một đống riêng tư chứa tất cả các đối tượng Python và cấu trúc dữ liệu. Việc quản lý đống riêng tư này được đảm bảo trong nội bộ bởi Trình quản lý bộ nhớ Python. Trình quản lý bộ nhớ Python có các thành phần khác nhau liên quan đến các khía cạnh quản lý lưu trữ động khác nhau, như chia sẻ, phân khúc, phân tách hoặc bộ nhớ đệm.

Ở cấp độ thấp nhất, bộ phân bổ bộ nhớ thô đảm bảo rằng có đủ chỗ trong đống riêng để lưu trữ tất cả dữ liệu liên quan đến Python bằng cách tương tác với trình quản lý bộ nhớ của hệ điều hành. Ngoài bộ phân bổ bộ nhớ thô, một số bộ phân bổ dành riêng cho đối tượng hoạt động trên cùng một đống và thực hiện các chính sách quản lý bộ nhớ riêng biệt thích nghi với đặc thù của mọi loại đối tượng. Ví dụ, các đối tượng số nguyên được quản lý khác nhau trong đống so với chuỗi, bộ dữ liệu hoặc từ điển vì các số nguyên ngụ ý các yêu cầu lưu trữ khác nhau và đánh đổi tốc độ/không gian. Do đó, Trình quản lý bộ nhớ Python ủy thác một số công việc cho các phân bổ dành riêng cho đối tượng, nhưng đảm bảo rằng phần sau hoạt động trong giới hạn của đống riêng tư.

Điều quan trọng là phải hiểu rằng việc quản lý đống Python được thực hiện bởi chính người phiên dịch và người dùng không có quyền kiểm soát nó, ngay cả khi họ thường xuyên thao túng các con trỏ đối tượng vào các khối bộ nhớ bên trong đống đó. Việc phân bổ không gian heap cho các đối tượng Python và các bộ đệm nội bộ khác được thực hiện theo yêu cầu của Trình quản lý bộ nhớ Python thông qua các hàm API Python/C được liệt kê trong tài liệu này.

Để tránh tham nhũng bộ nhớ, các nhà văn mở rộng không bao giờ nên cố gắng hoạt động trên các đối tượng Python với các chức năng được xuất bởi thư viện C: malloc(), calloc(), realloc()free(). Điều này sẽ dẫn đến các cuộc gọi hỗn hợp giữa Trình phân bổ C và Trình quản lý bộ nhớ Python với hậu quả nghiêm trọng, bởi vì họ thực hiện các thuật toán khác nhau và hoạt động trên các đống khác nhau. Tuy nhiên, người ta có thể phân bổ và giải phóng các khối bộ nhớ một cách an toàn với Trình phân bổ thư viện C cho các mục đích riêng lẻ, như được hiển thị trong ví dụ sau:

PyObject *res;
char *buf = (char *) malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
...Do some I/O operation involving buf...
res = PyBytes_FromString(buf);
free(buf); /* malloc'ed */
return res;

Trong ví dụ này, yêu cầu bộ nhớ cho bộ đệm I/O được xử lý bởi Trình phân bổ thư viện C. Trình quản lý bộ nhớ Python chỉ tham gia vào việc phân bổ đối tượng byte được trả về.

Tuy nhiên, trong hầu hết các tình huống, nên phân bổ bộ nhớ từ đống Python cụ thể vì cái sau nằm dưới sự kiểm soát của Trình quản lý bộ nhớ Python. Ví dụ: điều này là bắt buộc khi trình thông dịch được mở rộng với các loại đối tượng mới được viết bằng C. Một lý do khác để sử dụng heap Python là mong muốn thông báo cho Trình quản lý bộ nhớ Python về nhu cầu bộ nhớ của mô -đun mở rộng. Ngay cả khi bộ nhớ được yêu cầu được sử dụng riêng cho các mục đích nội bộ, cụ thể cao, việc ủy ​​thác tất cả các yêu cầu bộ nhớ cho Trình quản lý bộ nhớ Python khiến trình thông dịch có toàn bộ hình ảnh chính xác hơn về dấu chân bộ nhớ của nó. Do đó, trong một số trường hợp nhất định, Trình quản lý bộ nhớ Python có thể hoặc không thể kích hoạt các hành động thích hợp, như thu gom rác, nén bộ nhớ hoặc các thủ tục phòng ngừa khác. Lưu ý rằng bằng cách sử dụng trình phân bổ thư viện C như trong ví dụ trước, bộ nhớ được phân bổ cho bộ đệm I/O thoát hoàn toàn Trình quản lý bộ nhớ Python.

Xem thêm

Biến môi trường PYTHONMALLOC có thể được sử dụng để định cấu hình các bộ phân bổ bộ nhớ được sử dụng bởi Python.PYTHONMALLOC environment variable can be used to configure the memory allocators used by Python.

Biến môi trường PYTHONMALLOCSTATS có thể được sử dụng để in số liệu thống kê của bộ phân bổ bộ nhớ Pymalloc mỗi khi một đấu trường đối tượng Pymalloc mới được tạo và khi tắt máy.PYTHONMALLOCSTATS environment variable can be used to print statistics of the pymalloc memory allocator every time a new pymalloc object arena is created, and on shutdown.

Miền phân cấp

Tất cả các chức năng phân bổ thuộc về một trong ba tên miền khác nhau (xem thêm ____10). Các lĩnh vực này đại diện cho các chiến lược phân bổ khác nhau và được tối ưu hóa cho các mục đích khác nhau. Các chi tiết cụ thể về cách mọi miền phân bổ bộ nhớ hoặc các chức năng bên trong mà mỗi miền gọi được coi là một chi tiết triển khai, nhưng với mục đích gỡ lỗi, một bảng đơn giản có thể được tìm thấy tại đây. Không có yêu cầu khó khăn để sử dụng bộ nhớ được trả về bởi các hàm phân bổ thuộc về một miền nhất định chỉ cho các mục đích được gợi ý bởi miền đó (mặc dù đây là thực tiễn được đề xuất). Ví dụ, người ta có thể sử dụng bộ nhớ được trả về bởi

PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
1 để phân bổ các đối tượng Python hoặc bộ nhớ được trả về bởi
PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
2 để phân bổ bộ nhớ cho bộ đệm.here. There is no hard requirement to use the memory returned by the allocation functions belonging to a given domain for only the purposes hinted by that domain (although this is the recommended practice). For example, one could use the memory returned by
PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
1 for allocating Python objects or the memory returned by
PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
2 for allocating memory for buffers.

Ba miền phân bổ là:

  • Tên miền RAW: Dự định phân bổ bộ nhớ cho bộ đệm bộ nhớ có mục đích chung trong đó phân bổ phải đi đến trình phân bổ hệ thống hoặc nơi bộ phân bổ có thể hoạt động mà không cần Gil. Bộ nhớ được yêu cầu trực tiếp đến hệ thống.GIL. The memory is requested directly to the system.

  • Tên miền MEM MEM: dự định phân bổ bộ nhớ cho bộ đệm python và bộ đệm bộ nhớ đa năng trong đó phân bổ phải được thực hiện với GIL được giữ. Bộ nhớ được lấy từ đống riêng Python.GIL held. The memory is taken from the Python private heap.

  • Miền đối tượng: dự định phân bổ bộ nhớ thuộc về các đối tượng Python. Bộ nhớ được lấy từ đống riêng Python.

Khi giải phóng bộ nhớ được phân bổ trước đây bởi các hàm phân bổ thuộc một miền nhất định, phải sử dụng các chức năng giải quyết cụ thể phù hợp. Ví dụ,

PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
3 phải được sử dụng để phân bổ bộ nhớ miễn phí bằng
PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
4.

Giao diện bộ nhớ thô

Các bộ chức năng sau đây là trình bao bọc cho bộ phân bổ hệ thống. Các chức năng này là an toàn cho luồng, GIL không cần phải được giữ.GIL does not need to be held.

Bộ phân bổ bộ nhớ thô mặc định sử dụng các chức năng sau: malloc(), calloc(), realloc()free(); Gọi

PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
9 (hoặc
PyObject *res;
char *buf = PyMem_New(char, BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Del(buf); /* allocated with PyMem_New */
return res;
0) khi yêu cầu 0 byte.default raw memory allocator uses the following functions: malloc(), calloc(), realloc() and free(); call
PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
9 (or
PyObject *res;
char *buf = PyMem_New(char, BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Del(buf); /* allocated with PyMem_New */
return res;
0) when requesting zero bytes.

Mới trong phiên bản 3.4.

void* pymem_rawmalloc (size_tn) ¶allobated n byte và trả về một con trỏ khoảng trống* cho bộ nhớ được phân bổ hoặc
PyObject *res;
char *buf = PyMem_New(char, BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Del(buf); /* allocated with PyMem_New */
return res;
1 nếu yêu cầu không thành công.
*PyMem_RawMalloc(size_tn)

Allocates n bytes and returns a pointer of type void* to the allocated memory, or

PyObject *res;
char *buf = PyMem_New(char, BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Del(buf); /* allocated with PyMem_New */
return res;
1 if the request fails.

Yêu cầu số byte không trả về một con trỏ không khác biệt 21 nếu có thể, như thể

PyObject *res;
char *buf = PyMem_New(char, BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Del(buf); /* allocated with PyMem_New */
return res;
3 đã được gọi thay thế. Bộ nhớ sẽ không được khởi tạo theo bất kỳ cách nào.

void* pymem_rawcalloc (size_tnelem, size_telsize) ướp phân các phần tử nelem mỗi phần có kích thước trong byte là elsize và trả về một con trỏ khoảng trống* cho bộ nhớ được phân bổ, hoặc
PyObject *res;
char *buf = PyMem_New(char, BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Del(buf); /* allocated with PyMem_New */
return res;
1 nếu yêu cầu không thành công. Bộ nhớ được khởi tạo thành số không.
*PyMem_RawCalloc(size_tnelem, size_telsize)

Allocates nelem elements each whose size in bytes is elsize and returns a pointer of type void* to the allocated memory, or

PyObject *res;
char *buf = PyMem_New(char, BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Del(buf); /* allocated with PyMem_New */
return res;
1 if the request fails. The memory is initialized to zeros.

Yêu cầu các phần tử bằng 0 hoặc các phần tử có kích thước 0 byte trả về một con trỏ không khác biệt khác nhau nếu có thể, như thể

PyObject *res;
char *buf = PyMem_New(char, BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Del(buf); /* allocated with PyMem_New */
return res;
6 đã được gọi thay thế.

Mới trong phiên bản 3.5.

void*pymem_rawrealloc (void*p, size_tn) ¶resize khối bộ nhớ được trỏ bởi p đến n byte. Các nội dung sẽ không thay đổi ở mức tối thiểu của cũ và kích thước mới. *PyMem_RawRealloc(void*p, size_tn)

Resizes the memory block pointed to by p to n bytes. The contents will be unchanged to the minimum of the old and the new sizes.

Nếu P là

PyObject *res;
char *buf = PyMem_New(char, BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Del(buf); /* allocated with PyMem_New */
return res;
1, cuộc gọi tương đương với
PyObject *res;
char *buf = PyMem_New(char, BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Del(buf); /* allocated with PyMem_New */
return res;
8; khác nếu N bằng 0, khối bộ nhớ được thay đổi kích thước nhưng không được giải phóng và con trỏ trả về không phải là -________ 21.

Trừ khi p là

PyObject *res;
char *buf = PyMem_New(char, BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Del(buf); /* allocated with PyMem_New */
return res;
1, nó phải được trả lại bằng một cuộc gọi trước đó đến
PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
1,
char *buf1 = PyMem_New(char, BUFSIZ);
char *buf2 = (char *) malloc(BUFSIZ);
char *buf3 = (char *) PyMem_Malloc(BUFSIZ);
...
PyMem_Del(buf3);  /* Wrong -- should be PyMem_Free() */
free(buf2);       /* Right -- allocated via malloc() */
free(buf1);       /* Fatal -- should be PyMem_Del()  */
2 hoặc
char *buf1 = PyMem_New(char, BUFSIZ);
char *buf2 = (char *) malloc(BUFSIZ);
char *buf3 = (char *) PyMem_Malloc(BUFSIZ);
...
PyMem_Del(buf3);  /* Wrong -- should be PyMem_Free() */
free(buf2);       /* Right -- allocated via malloc() */
free(buf1);       /* Fatal -- should be PyMem_Del()  */
3.

Nếu yêu cầu không thành công,

char *buf1 = PyMem_New(char, BUFSIZ);
char *buf2 = (char *) malloc(BUFSIZ);
char *buf3 = (char *) PyMem_Malloc(BUFSIZ);
...
PyMem_Del(buf3);  /* Wrong -- should be PyMem_Free() */
free(buf2);       /* Right -- allocated via malloc() */
free(buf1);       /* Fatal -- should be PyMem_Del()  */
2 sẽ trả về
PyObject *res;
char *buf = PyMem_New(char, BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Del(buf); /* allocated with PyMem_New */
return res;
1 và P vẫn là một con trỏ hợp lệ đến khu vực bộ nhớ trước.

VoidPymem_Rawfree (void*p) ¶Frees khối bộ nhớ được trỏ bởi P, điều này phải được trả lại bởi một cuộc gọi trước đó đến
PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
1,
char *buf1 = PyMem_New(char, BUFSIZ);
char *buf2 = (char *) malloc(BUFSIZ);
char *buf3 = (char *) PyMem_Malloc(BUFSIZ);
...
PyMem_Del(buf3);  /* Wrong -- should be PyMem_Free() */
free(buf2);       /* Right -- allocated via malloc() */
free(buf1);       /* Fatal -- should be PyMem_Del()  */
2 hoặc
char *buf1 = PyMem_New(char, BUFSIZ);
char *buf2 = (char *) malloc(BUFSIZ);
char *buf3 = (char *) PyMem_Malloc(BUFSIZ);
...
PyMem_Del(buf3);  /* Wrong -- should be PyMem_Free() */
free(buf2);       /* Right -- allocated via malloc() */
free(buf1);       /* Fatal -- should be PyMem_Del()  */
3. Nếu không, hoặc nếu
char *buf1 = PyMem_New(char, BUFSIZ);
char *buf2 = (char *) malloc(BUFSIZ);
char *buf3 = (char *) PyMem_Malloc(BUFSIZ);
...
PyMem_Del(buf3);  /* Wrong -- should be PyMem_Free() */
free(buf2);       /* Right -- allocated via malloc() */
free(buf1);       /* Fatal -- should be PyMem_Del()  */
9 đã được gọi trước đó, hành vi không xác định xảy ra.
PyMem_RawFree(void*p)

Frees the memory block pointed to by p, which must have been returned by a previous call to

PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
1,
char *buf1 = PyMem_New(char, BUFSIZ);
char *buf2 = (char *) malloc(BUFSIZ);
char *buf3 = (char *) PyMem_Malloc(BUFSIZ);
...
PyMem_Del(buf3);  /* Wrong -- should be PyMem_Free() */
free(buf2);       /* Right -- allocated via malloc() */
free(buf1);       /* Fatal -- should be PyMem_Del()  */
2 or
char *buf1 = PyMem_New(char, BUFSIZ);
char *buf2 = (char *) malloc(BUFSIZ);
char *buf3 = (char *) PyMem_Malloc(BUFSIZ);
...
PyMem_Del(buf3);  /* Wrong -- should be PyMem_Free() */
free(buf2);       /* Right -- allocated via malloc() */
free(buf1);       /* Fatal -- should be PyMem_Del()  */
3. Otherwise, or if
char *buf1 = PyMem_New(char, BUFSIZ);
char *buf2 = (char *) malloc(BUFSIZ);
char *buf3 = (char *) PyMem_Malloc(BUFSIZ);
...
PyMem_Del(buf3);  /* Wrong -- should be PyMem_Free() */
free(buf2);       /* Right -- allocated via malloc() */
free(buf1);       /* Fatal -- should be PyMem_Del()  */
9 has been called before, undefined behavior occurs.

Nếu P là

PyObject *res;
char *buf = PyMem_New(char, BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Del(buf); /* allocated with PyMem_New */
return res;
1, không có hoạt động nào được thực hiện.

Giao diện bộ nhớ

Các bộ chức năng sau, được mô hình hóa theo tiêu chuẩn ANSI C, nhưng chỉ định hành vi khi yêu cầu 0 byte, có sẵn để phân bổ và giải phóng bộ nhớ từ đống Python.

Bộ phân bổ bộ nhớ mặc định sử dụng bộ phân bổ bộ nhớ Pymalloc.default memory allocator uses the pymalloc memory allocator.

Cảnh báo

GIL phải được giữ khi sử dụng các chức năng này.GIL must be held when using these functions.

Đã thay đổi trong phiên bản 3.6: Bộ phân bổ mặc định hiện là pymalloc thay vì hệ thống malloc().The default allocator is now pymalloc instead of system malloc().

void*pymem_malloc (size_tn) một phần của ABI ổn định.*PyMem_Malloc(size_tn)
Part of the Stable ABI.

Phân bổ n byte và trả về một con trỏ khoảng trống* cho bộ nhớ được phân bổ hoặc

PyObject *res;
char *buf = PyMem_New(char, BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Del(buf); /* allocated with PyMem_New */
return res;
1 nếu yêu cầu không thành công.void* to the allocated memory, or
PyObject *res;
char *buf = PyMem_New(char, BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Del(buf); /* allocated with PyMem_New */
return res;
1 if the request fails.

Yêu cầu Zero Byte trả về một con trỏ không khác biệt 21 nếu có thể, như thể malloc()4 đã được gọi thay thế. Bộ nhớ sẽ không được khởi tạo theo bất kỳ cách nào.

void*pymem_calloc (size_tnelem, size_telsize) một phần của ABI ổn định kể từ phiên bản 3.7.*PyMem_Calloc(size_tnelem, size_telsize)
Part of the Stable ABI since version 3.7.

Phân bổ các phần tử nelem, mỗi phần tử có kích thước trong byte là elsize và trả về một con trỏ khoảng trống* cho bộ nhớ được phân bổ hoặc

PyObject *res;
char *buf = PyMem_New(char, BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Del(buf); /* allocated with PyMem_New */
return res;
1 nếu yêu cầu không thành công. Bộ nhớ được khởi tạo thành số không.void* to the allocated memory, or
PyObject *res;
char *buf = PyMem_New(char, BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Del(buf); /* allocated with PyMem_New */
return res;
1 if the request fails. The memory is initialized to zeros.

Yêu cầu các phần tử bằng 0 hoặc các phần tử có kích thước 0 byte trả về một con trỏ không khác biệt khác nhau nếu có thể, như thể malloc()7 đã được gọi thay thế.

Mới trong phiên bản 3.5.

void*pymem_rawrealloc (void*p, size_tn) ¶resize khối bộ nhớ được trỏ bởi p đến n byte. Các nội dung sẽ không thay đổi ở mức tối thiểu của cũ và kích thước mới.*PyMem_Realloc(void*p, size_t n)
Part of the Stable ABI.

Nếu P là

PyObject *res;
char *buf = PyMem_New(char, BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Del(buf); /* allocated with PyMem_New */
return res;
1, cuộc gọi tương đương với
PyObject *res;
char *buf = PyMem_New(char, BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Del(buf); /* allocated with PyMem_New */
return res;
8; khác nếu N bằng 0, khối bộ nhớ được thay đổi kích thước nhưng không được giải phóng và con trỏ trả về không phải là -________ 21.

Trừ khi p là

PyObject *res;
char *buf = PyMem_New(char, BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Del(buf); /* allocated with PyMem_New */
return res;
1, nó phải được trả lại bằng một cuộc gọi trước đó đến
PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
1,
char *buf1 = PyMem_New(char, BUFSIZ);
char *buf2 = (char *) malloc(BUFSIZ);
char *buf3 = (char *) PyMem_Malloc(BUFSIZ);
...
PyMem_Del(buf3);  /* Wrong -- should be PyMem_Free() */
free(buf2);       /* Right -- allocated via malloc() */
free(buf1);       /* Fatal -- should be PyMem_Del()  */
2 hoặc
char *buf1 = PyMem_New(char, BUFSIZ);
char *buf2 = (char *) malloc(BUFSIZ);
char *buf3 = (char *) PyMem_Malloc(BUFSIZ);
...
PyMem_Del(buf3);  /* Wrong -- should be PyMem_Free() */
free(buf2);       /* Right -- allocated via malloc() */
free(buf1);       /* Fatal -- should be PyMem_Del()  */
3.

Nếu yêu cầu không thành công,

char *buf1 = PyMem_New(char, BUFSIZ);
char *buf2 = (char *) malloc(BUFSIZ);
char *buf3 = (char *) PyMem_Malloc(BUFSIZ);
...
PyMem_Del(buf3);  /* Wrong -- should be PyMem_Free() */
free(buf2);       /* Right -- allocated via malloc() */
free(buf1);       /* Fatal -- should be PyMem_Del()  */
2 sẽ trả về
PyObject *res;
char *buf = PyMem_New(char, BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Del(buf); /* allocated with PyMem_New */
return res;
1 và P vẫn là một con trỏ hợp lệ đến khu vực bộ nhớ trước.

VoidPymem_Rawfree (void*p) ¶Frees khối bộ nhớ được trỏ bởi P, điều này phải được trả lại bởi một cuộc gọi trước đó đến

PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
1,
char *buf1 = PyMem_New(char, BUFSIZ);
char *buf2 = (char *) malloc(BUFSIZ);
char *buf3 = (char *) PyMem_Malloc(BUFSIZ);
...
PyMem_Del(buf3);  /* Wrong -- should be PyMem_Free() */
free(buf2);       /* Right -- allocated via malloc() */
free(buf1);       /* Fatal -- should be PyMem_Del()  */
2 hoặc
char *buf1 = PyMem_New(char, BUFSIZ);
char *buf2 = (char *) malloc(BUFSIZ);
char *buf3 = (char *) PyMem_Malloc(BUFSIZ);
...
PyMem_Del(buf3);  /* Wrong -- should be PyMem_Free() */
free(buf2);       /* Right -- allocated via malloc() */
free(buf1);       /* Fatal -- should be PyMem_Del()  */
3. Nếu không, hoặc nếu
char *buf1 = PyMem_New(char, BUFSIZ);
char *buf2 = (char *) malloc(BUFSIZ);
char *buf3 = (char *) PyMem_Malloc(BUFSIZ);
...
PyMem_Del(buf3);  /* Wrong -- should be PyMem_Free() */
free(buf2);       /* Right -- allocated via malloc() */
free(buf1);       /* Fatal -- should be PyMem_Del()  */
9 đã được gọi trước đó, hành vi không xác định xảy ra.

Nếu P là
PyObject *res;
char *buf = PyMem_New(char, BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Del(buf); /* allocated with PyMem_New */
return res;
1, không có hoạt động nào được thực hiện.
PyMem_Free(void*p)
Part of the Stable ABI.

Giao diện bộ nhớ

Nếu P là

PyObject *res;
char *buf = PyMem_New(char, BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Del(buf); /* allocated with PyMem_New */
return res;
1, không có hoạt động nào được thực hiện.

Các macro định hướng loại sau được cung cấp để thuận tiện. Lưu ý rằng loại đề cập đến bất kỳ loại C nào.

Loại*pymem_new (gõ, size_tn) ¶ameSame dưới dạng
PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
4, nhưng phân bổ realloc()3 byte của bộ nhớ. Trả về một con trỏ đúc thành loại*. Bộ nhớ sẽ không được khởi tạo theo bất kỳ cách nào.
*PyMem_New(TYPE, size_t n)

Same as

PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
4, but allocates realloc()3 bytes of memory. Returns a pointer cast to TYPE*. The memory will not have been initialized in any way.

Loại*pymem_resize (void*p, gõ, size_tn) ¶ame dưới dạng calloc()3, nhưng khối bộ nhớ được thay đổi kích thước thành realloc()5 byte. Trả về một con trỏ đúc thành loại*. Khi trở về, P sẽ là một con trỏ đến khu vực bộ nhớ mới hoặc
PyObject *res;
char *buf = PyMem_New(char, BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Del(buf); /* allocated with PyMem_New */
return res;
1 trong trường hợp thất bại.
*PyMem_Resize(void*p, TYPE, size_t n)

Same as calloc()3, but the memory block is resized to realloc()5 bytes. Returns a pointer cast to TYPE*. On return, p will be a pointer to the new memory area, or

PyObject *res;
char *buf = PyMem_New(char, BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Del(buf); /* allocated with PyMem_New */
return res;
1 in the event of failure.

Đây là một macro tiền xử lý C; P luôn được chỉ định lại. Lưu giá trị ban đầu của P để tránh mất bộ nhớ khi xử lý lỗi.

VoidPymem_Del (void*p) ¶ameSme là
PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
3.
PyMem_Del(void *p)

Same as

PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
3.

Ngoài ra, các bộ macro sau được cung cấp để gọi trực tiếp bộ phân bổ bộ nhớ Python, mà không liên quan đến các hàm A API được liệt kê ở trên. Tuy nhiên, lưu ý rằng việc sử dụng của chúng không bảo tồn khả năng tương thích nhị phân trên các phiên bản Python và do đó không được dùng trong các mô -đun mở rộng.

  • realloc()8

  • realloc()9

  • free()0

  • free()1

  • free()2

  • free()3

Người phân bổ đối tượng

Các bộ chức năng sau, được mô hình hóa theo tiêu chuẩn ANSI C, nhưng chỉ định hành vi khi yêu cầu 0 byte, có sẵn để phân bổ và giải phóng bộ nhớ từ đống Python.

Ghi chú

Không có gì đảm bảo rằng bộ nhớ được trả về bởi các phân bổ này có thể được chuyển thành công vào một đối tượng Python khi chặn các hàm phân bổ trong miền này bằng các phương thức được mô tả trong phần phân bổ bộ nhớ tùy chỉnh.Customize Memory Allocators section.

Trình phân bổ đối tượng mặc định sử dụng Trình phân bổ bộ nhớ Pymalloc.default object allocator uses the pymalloc memory allocator.

Cảnh báo

GIL phải được giữ khi sử dụng các chức năng này.GIL must be held when using these functions.

void*pyobject_malloc (size_tn) một phần của ABI ổn định.*PyObject_Malloc(size_t n)
Part of the Stable ABI.

Phân bổ n byte và trả về một con trỏ khoảng trống* cho bộ nhớ được phân bổ hoặc

PyObject *res;
char *buf = PyMem_New(char, BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Del(buf); /* allocated with PyMem_New */
return res;
1 nếu yêu cầu không thành công.void* to the allocated memory, or
PyObject *res;
char *buf = PyMem_New(char, BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Del(buf); /* allocated with PyMem_New */
return res;
1 if the request fails.

Yêu cầu Zero Byte trả về một con trỏ không khác biệt 21 nếu có thể, như thể free()6 đã được gọi thay thế. Bộ nhớ sẽ không được khởi tạo theo bất kỳ cách nào.

void*PyObject_Calloc (size_tnelem, size_telsize) Một phần của ABI ổn định kể từ phiên bản 3.7.*PyObject_Calloc(size_tnelem, size_t elsize)
Part of the Stable ABI since version 3.7.

Phân bổ các phần tử nelem, mỗi phần tử có kích thước trong byte là elsize và trả về một con trỏ khoảng trống* cho bộ nhớ được phân bổ hoặc

PyObject *res;
char *buf = PyMem_New(char, BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Del(buf); /* allocated with PyMem_New */
return res;
1 nếu yêu cầu không thành công. Bộ nhớ được khởi tạo thành số không.void* to the allocated memory, or
PyObject *res;
char *buf = PyMem_New(char, BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Del(buf); /* allocated with PyMem_New */
return res;
1 if the request fails. The memory is initialized to zeros.

Yêu cầu các phần tử bằng 0 hoặc các phần tử có kích thước 0 byte trả về một con trỏ không khác biệt khác nhau nếu có thể, như thể free()9 đã được gọi thay thế.

Mới trong phiên bản 3.5.

void*pyobject_realloc (void*p, size_tn) một phần của abi ổn định. *PyObject_Realloc(void*p, size_tn)
Part of the Stable ABI.

Thay đổi kích thước khối bộ nhớ được trỏ bởi p thành n byte. Các nội dung sẽ không thay đổi ở mức tối thiểu của cũ và kích thước mới.

Nếu P là

PyObject *res;
char *buf = PyMem_New(char, BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Del(buf); /* allocated with PyMem_New */
return res;
1, cuộc gọi tương đương với PYTHONMALLOC1; khác nếu N bằng 0, khối bộ nhớ được thay đổi kích thước nhưng không được giải phóng và con trỏ trả về không phải là -________ 21.

Trừ khi P là

PyObject *res;
char *buf = PyMem_New(char, BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Del(buf); /* allocated with PyMem_New */
return res;
1, nó phải được trả lại bằng một cuộc gọi trước đó cho
PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
2, PYTHONMALLOC5 hoặc PYTHONMALLOC6.

Nếu yêu cầu không thành công, PYTHONMALLOC5 sẽ trả về

PyObject *res;
char *buf = PyMem_New(char, BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Del(buf); /* allocated with PyMem_New */
return res;
1 và P vẫn là một con trỏ hợp lệ đến khu vực bộ nhớ trước.

VoidPyObject_Free (void*p) Một phần của ABI ổn định.PyObject_Free(void *p)
Part of the Stable ABI.

Giải phóng khối bộ nhớ được chỉ bằng P, phải được trả lại bằng một cuộc gọi trước đó đến

PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
2, PYTHONMALLOC5 hoặc PYTHONMALLOC6. Nếu không, hoặc nếu PYTHONMALLOCSTATS2 đã được gọi trước đó, hành vi không xác định xảy ra.

Nếu P là

PyObject *res;
char *buf = PyMem_New(char, BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Del(buf); /* allocated with PyMem_New */
return res;
1, không có hoạt động nào được thực hiện.

Phân bổ bộ nhớ mặc định

Bộ phân bổ bộ nhớ mặc định:

Cấu hình

Tên

PyMem_RawMalloc

PyMem_Malloc

PyObject_Malloc

Phát hành bản dựng

PYTHONMALLOCSTATS4

PYTHONMALLOCSTATS5

PYTHONMALLOCSTATS6

PYTHONMALLOCSTATS6

Gỡ lỗi xây dựng

PYTHONMALLOCSTATS8

PYTHONMALLOCSTATS5 + gỡ lỗi

PYTHONMALLOCSTATS6 + gỡ lỗi

PYTHONMALLOCSTATS6 + gỡ lỗi

Phát hành bản dựng, không có pymalloc

PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
02

PYTHONMALLOCSTATS5

PYTHONMALLOCSTATS5

PYTHONMALLOCSTATS5

Gỡ lỗi xây dựng, không có pymalloc

PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
06

PYTHONMALLOCSTATS5 + gỡ lỗi

PYTHONMALLOCSTATS5 + gỡ lỗi

PYTHONMALLOCSTATS5 + gỡ lỗi

Legend:

  • PYTHONMALLOCSTATS6 + gỡ lỗiPYTHONMALLOC environment variable.

  • Phát hành bản dựng, không có pymalloc

  • Gỡ lỗi xây dựng, không có pymallocpymalloc memory allocator.

  • Tên: Giá trị cho biến môi trường PYTHONMALLOC.debug hooks on the Python memory allocators.

  • PYTHONMALLOCSTATS5: Bộ phân bổ hệ thống từ thư viện C tiêu chuẩn, hàm C: malloc(), calloc(), realloc()free().Python build in debug mode.

PYTHONMALLOCSTATS6: Bộ phân bổ bộ nhớ Pymalloc.

Cấm+ DEBUG: với các móc gỡ lỗi trên các bộ phân bổ bộ nhớ Python.

Build Debug Build Build: Build Python trong chế độ gỡ lỗi.PyMemAllocatorEx

Structure used to describe a memory block allocator. The structure has the following fields:

Tùy chỉnh bộ phận phân bổ bộ nhớ

Mới trong phiên bản 3.4.

PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
17

TypePymemallocatorex¶ cấu trúc được sử dụng để mô tả bộ phân bổ khối bộ nhớ. Cấu trúc có các trường sau:

PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
18

Đồng ruộng

PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
19

Nghĩa

PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
20

Bối cảnh người dùng được thông qua như đối số đầu tiên

PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
21

Phân bổ một khối bộ nhớ

Phân bổ một khối bộ nhớ được khởi tạo bằng các số khôngThe

PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
22 structure was renamed to
PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
23 and a new
PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
24 field was added.

TypePyMemallocatordomain¶enum được sử dụng để xác định miền phân bổ. Miền: PyMemAllocatorDomain

Enum used to identify an allocator domain. Domains:

Pymem_domain_raw¶fnes:

Functions:

  • PyObject *res;
    char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */
    
    if (buf == NULL)
        return PyErr_NoMemory();
    /* ...Do some I/O operation involving buf... */
    res = PyBytes_FromString(buf);
    PyMem_Free(buf); /* allocated with PyMem_Malloc */
    return res;
    
    1

  • char *buf1 = PyMem_New(char, BUFSIZ);
    char *buf2 = (char *) malloc(BUFSIZ);
    char *buf3 = (char *) PyMem_Malloc(BUFSIZ);
    ...
    PyMem_Del(buf3);  /* Wrong -- should be PyMem_Free() */
    free(buf2);       /* Right -- allocated via malloc() */
    free(buf1);       /* Fatal -- should be PyMem_Del()  */
    
    2

  • char *buf1 = PyMem_New(char, BUFSIZ);
    char *buf2 = (char *) malloc(BUFSIZ);
    char *buf3 = (char *) PyMem_Malloc(BUFSIZ);
    ...
    PyMem_Del(buf3);  /* Wrong -- should be PyMem_Free() */
    free(buf2);       /* Right -- allocated via malloc() */
    free(buf1);       /* Fatal -- should be PyMem_Del()  */
    
    3

  • PyObject *res;
    char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */
    
    if (buf == NULL)
        return PyErr_NoMemory();
    /* ...Do some I/O operation involving buf... */
    res = PyBytes_FromString(buf);
    PyMem_Free(buf); /* allocated with PyMem_Malloc */
    return res;
    
    28

Pymem_domain_mem¶fnes:

Functions:

  • PyObject *res;
    char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */
    
    if (buf == NULL)
        return PyErr_NoMemory();
    /* ...Do some I/O operation involving buf... */
    res = PyBytes_FromString(buf);
    PyMem_Free(buf); /* allocated with PyMem_Malloc */
    return res;
    
    4,

  • calloc()3

  • calloc()4

  • PyObject *res;
    char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */
    
    if (buf == NULL)
        return PyErr_NoMemory();
    /* ...Do some I/O operation involving buf... */
    res = PyBytes_FromString(buf);
    PyMem_Free(buf); /* allocated with PyMem_Malloc */
    return res;
    
    3

Pymem_domain_obj¶fnes:

Functions:

  • PyObject *res;
    char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */
    
    if (buf == NULL)
        return PyErr_NoMemory();
    /* ...Do some I/O operation involving buf... */
    res = PyBytes_FromString(buf);
    PyMem_Free(buf); /* allocated with PyMem_Malloc */
    return res;
    
    2

  • PYTHONMALLOC5

  • PYTHONMALLOC6

  • PyObject *res;
    char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */
    
    if (buf == NULL)
        return PyErr_NoMemory();
    /* ...Do some I/O operation involving buf... */
    res = PyBytes_FromString(buf);
    PyMem_Free(buf); /* allocated with PyMem_Malloc */
    return res;
    
    36

VoidPymem_Getallocator (pymemallocatordomaindomain, pymemallocatorex*ALLOCATOR) Ăn bộ phân bổ khối bộ nhớ của miền được chỉ định.PyMem_GetAllocator(PyMemAllocatorDomaindomain, PyMemAllocatorEx *allocator)

Get the memory block allocator of the specified domain.

Voidpymem_setallocator (PymemallocatorDomAINDOMAIN, PYMEMALLOCATOREX*ALLOCATOR) ¶ Set Trình phân bổ khối bộ nhớ của miền được chỉ định. PyMem_SetAllocator(PyMemAllocatorDomaindomain, PyMemAllocatorEx *allocator)

Set the memory block allocator of the specified domain.

Bộ phân bổ mới phải trả về một con trỏ không khác nhau khi yêu cầu số 0 byte.

Đối với miền

PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
38, bộ phân bổ phải an toàn cho luồng: GIL không được giữ khi bộ phân bổ được gọi.GIL is not held when the allocator is called.

Nếu bộ phân bổ mới không phải là một móc (không gọi trình phân bổ trước đó), hàm

PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
39 phải được gọi để cài đặt lại các móc gỡ lỗi trên đầu trên trình phân bổ mới.

Xem thêm

PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
40 và preinitialize python với pypreconfig.Preinitialize Python with PyPreConfig.

Cảnh báo

PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
41 có hợp đồng sau:

  • Nó có thể được gọi sau

    PyObject *res;
    char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */
    
    if (buf == NULL)
        return PyErr_NoMemory();
    /* ...Do some I/O operation involving buf... */
    res = PyBytes_FromString(buf);
    PyMem_Free(buf); /* allocated with PyMem_Malloc */
    return res;
    
    42 và trước
    PyObject *res;
    char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */
    
    if (buf == NULL)
        return PyErr_NoMemory();
    /* ...Do some I/O operation involving buf... */
    res = PyBytes_FromString(buf);
    PyMem_Free(buf); /* allocated with PyMem_Malloc */
    return res;
    
    43 để cài đặt bộ phân bổ bộ nhớ tùy chỉnh. Không có giới hạn đối với bộ phân bổ được cài đặt khác với các bộ phân bổ được áp đặt bởi tên miền (ví dụ, miền RAW cho phép bộ phân bổ được gọi mà không cần GiL giữ). Xem phần trên các miền phân bổ để biết thêm thông tin.the section on allocator domains for more information.

  • Nếu được gọi sau khi Python đã hoàn thành khởi tạo (sau khi

    PyObject *res;
    char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */
    
    if (buf == NULL)
        return PyErr_NoMemory();
    /* ...Do some I/O operation involving buf... */
    res = PyBytes_FromString(buf);
    PyMem_Free(buf); /* allocated with PyMem_Malloc */
    return res;
    
    43 được gọi), bộ phân bổ phải bọc bộ phân bổ hiện có. Thay thế bộ phân bổ hiện tại cho một số tùy ý khác không được hỗ trợ.must wrap the existing allocator. Substituting the current allocator for some other arbitrary one is not supported.

Voidpymem_setupdebughooks (void) ¶setUp Debug Hook trong các trình phân bổ bộ nhớ Python để phát hiện các lỗi bộ nhớ.PyMem_SetupDebugHooks(void)

Setup debug hooks in the Python memory allocators to detect memory errors.

Gỡ lỗi móc vào bộ phân bổ bộ nhớ Python

Khi Python được xây dựng ở chế độ gỡ lỗi, hàm

PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
39 được gọi tại Python Preinitialization để thiết lập các móc gỡ lỗi trên các bộ phân bổ bộ nhớ Python để phát hiện lỗi bộ nhớ.Python is built in debug mode, the
PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
39 function is called at the Python preinitialization to setup debug hooks on Python memory allocators to detect memory errors.

Biến môi trường PYTHONMALLOC có thể được sử dụng để cài đặt các móc gỡ lỗi trên một python được biên dịch trong chế độ phát hành (ví dụ:

PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
47).PYTHONMALLOC environment variable can be used to install debug hooks on a Python compiled in release mode (ex:
PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
47).

Hàm

PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
39 có thể được sử dụng để đặt các móc gỡ lỗi sau khi gọi
PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
41.

Các móc gỡ lỗi này lấp đầy các khối bộ nhớ được phân bổ động với các mẫu bit đặc biệt, dễ nhận biết. Bộ nhớ mới được phân bổ được lấp đầy bằng byte

PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
50 (
PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
51), bộ nhớ được giải phóng được lấp đầy bằng byte
PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
52 (
PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
53). Các khối bộ nhớ được bao quanh bởi các byte bị cấm của các byte, chứa đầy byte
PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
54 (
PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
55). Các chuỗi của các byte này khó có thể là các địa chỉ, phao hoặc chuỗi ASCII hợp lệ.

Kiểm tra thời gian chạy:

  • Phát hiện vi phạm API. Ví dụ: phát hiện nếu

    PyObject *res;
    char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */
    
    if (buf == NULL)
        return PyErr_NoMemory();
    /* ...Do some I/O operation involving buf... */
    res = PyBytes_FromString(buf);
    PyMem_Free(buf); /* allocated with PyMem_Malloc */
    return res;
    
    36 được gọi trên khối bộ nhớ được phân bổ bởi
    PyObject *res;
    char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */
    
    if (buf == NULL)
        return PyErr_NoMemory();
    /* ...Do some I/O operation involving buf... */
    res = PyBytes_FromString(buf);
    PyMem_Free(buf); /* allocated with PyMem_Malloc */
    return res;
    
    4.

  • Phát hiện ghi trước khi bắt đầu bộ đệm (bộ đệm dưới dòng).

  • Phát hiện ghi sau khi kết thúc bộ đệm (tràn bộ đệm).

  • Kiểm tra xem GIL có được giữ khi các hàm phân bổ của các miền

    PyObject *res;
    char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */
    
    if (buf == NULL)
        return PyErr_NoMemory();
    /* ...Do some I/O operation involving buf... */
    res = PyBytes_FromString(buf);
    PyMem_Free(buf); /* allocated with PyMem_Malloc */
    return res;
    
    58 (Xh:
    PyObject *res;
    char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */
    
    if (buf == NULL)
        return PyErr_NoMemory();
    /* ...Do some I/O operation involving buf... */
    res = PyBytes_FromString(buf);
    PyMem_Free(buf); /* allocated with PyMem_Malloc */
    return res;
    
    2) và
    PyObject *res;
    char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */
    
    if (buf == NULL)
        return PyErr_NoMemory();
    /* ...Do some I/O operation involving buf... */
    res = PyBytes_FromString(buf);
    PyMem_Free(buf); /* allocated with PyMem_Malloc */
    return res;
    
    60 (Xh:
    PyObject *res;
    char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */
    
    if (buf == NULL)
        return PyErr_NoMemory();
    /* ...Do some I/O operation involving buf... */
    res = PyBytes_FromString(buf);
    PyMem_Free(buf); /* allocated with PyMem_Malloc */
    return res;
    
    4) được gọi không.GIL is held when allocator functions of
    PyObject *res;
    char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */
    
    if (buf == NULL)
        return PyErr_NoMemory();
    /* ...Do some I/O operation involving buf... */
    res = PyBytes_FromString(buf);
    PyMem_Free(buf); /* allocated with PyMem_Malloc */
    return res;
    
    58 (ex:
    PyObject *res;
    char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */
    
    if (buf == NULL)
        return PyErr_NoMemory();
    /* ...Do some I/O operation involving buf... */
    res = PyBytes_FromString(buf);
    PyMem_Free(buf); /* allocated with PyMem_Malloc */
    return res;
    
    2) and
    PyObject *res;
    char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */
    
    if (buf == NULL)
        return PyErr_NoMemory();
    /* ...Do some I/O operation involving buf... */
    res = PyBytes_FromString(buf);
    PyMem_Free(buf); /* allocated with PyMem_Malloc */
    return res;
    
    60 (ex:
    PyObject *res;
    char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */
    
    if (buf == NULL)
        return PyErr_NoMemory();
    /* ...Do some I/O operation involving buf... */
    res = PyBytes_FromString(buf);
    PyMem_Free(buf); /* allocated with PyMem_Malloc */
    return res;
    
    4) domains are called.

Lỗi, các móc gỡ lỗi sử dụng mô -đun

PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
62 để có được dấu vết trong đó khối bộ nhớ được phân bổ. TraceBack chỉ được hiển thị nếu
PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
62 đang truy tìm phân bổ bộ nhớ python và khối bộ nhớ được truy tìm.

Đặt S =

PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
64.
PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
65 byte được thêm vào ở mỗi đầu của mỗi khối n byte được yêu cầu. Bố cục bộ nhớ giống như vậy, trong đó p đại diện cho địa chỉ được trả về bởi hàm giống như malloc hoặc giống như realloc (
PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
66 có nghĩa là lát byte từ
PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
67 bao gồm đến
PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
68 ):

PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
69

Số byte ban đầu yêu cầu. Đây là một size_t, Big-endian (dễ đọc hơn trong kết xuất bộ nhớ).

PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
70

Định danh API (ký tự ASCII):

  • PyObject *res;
    char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */
    
    if (buf == NULL)
        return PyErr_NoMemory();
    /* ...Do some I/O operation involving buf... */
    res = PyBytes_FromString(buf);
    PyMem_Free(buf); /* allocated with PyMem_Malloc */
    return res;
    
    71 cho
    PyObject *res;
    char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */
    
    if (buf == NULL)
        return PyErr_NoMemory();
    /* ...Do some I/O operation involving buf... */
    res = PyBytes_FromString(buf);
    PyMem_Free(buf); /* allocated with PyMem_Malloc */
    return res;
    
    38.

  • PyObject *res;
    char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */
    
    if (buf == NULL)
        return PyErr_NoMemory();
    /* ...Do some I/O operation involving buf... */
    res = PyBytes_FromString(buf);
    PyMem_Free(buf); /* allocated with PyMem_Malloc */
    return res;
    
    73 cho
    PyObject *res;
    char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */
    
    if (buf == NULL)
        return PyErr_NoMemory();
    /* ...Do some I/O operation involving buf... */
    res = PyBytes_FromString(buf);
    PyMem_Free(buf); /* allocated with PyMem_Malloc */
    return res;
    
    60.

  • PyObject *res;
    char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */
    
    if (buf == NULL)
        return PyErr_NoMemory();
    /* ...Do some I/O operation involving buf... */
    res = PyBytes_FromString(buf);
    PyMem_Free(buf); /* allocated with PyMem_Malloc */
    return res;
    
    75 cho
    PyObject *res;
    char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */
    
    if (buf == NULL)
        return PyErr_NoMemory();
    /* ...Do some I/O operation involving buf... */
    res = PyBytes_FromString(buf);
    PyMem_Free(buf); /* allocated with PyMem_Malloc */
    return res;
    
    58.

PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
77

Bản sao của pymem_forbiddenbyte. Được sử dụng để bắt dưới và đọc.

PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
78

Bộ nhớ được yêu cầu, chứa đầy các bản sao của pymem_cleanbyte, được sử dụng để bắt tham chiếu đến bộ nhớ không được hưởng. Khi một hàm giống như realloc được gọi là yêu cầu một khối bộ nhớ lớn hơn, các byte vượt quá mới cũng được lấp đầy bằng pymem_cleanbyte. Khi một hàm giống như tự do được gọi, chúng được ghi đè bằng pymem_deadbyte, để bắt tham chiếu đến bộ nhớ tự do. Khi một hàm giống như realloc được gọi là yêu cầu một khối bộ nhớ nhỏ hơn, các byte cũ dư thừa cũng được lấp đầy bằng pymem_deadbyte.

PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
79

Bản sao của pymem_forbiddenbyte. Được sử dụng để bắt quá mức và đọc.

PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
80

Chỉ được sử dụng nếu macro

PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
81 được xác định (không được xác định theo mặc định).

Một số sê-ri, được tăng thêm 1 trên mỗi cuộc gọi đến hàm giống như malloc hoặc giống như Realloc. Big-endian

PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
82. Nếu phát hiện ra bộ nhớ xấu của người Viking sau đó, số sê -ri sẽ đưa ra một cách tuyệt vời để đặt điểm dừng ở lần chạy tiếp theo, để nắm bắt ngay lập tức mà khối này được đưa ra. Hàm tĩnh bumpserialno () trong obmalloc.c là nơi duy nhất mà số sê -ri được tăng lên và tồn tại để bạn có thể đặt điểm dừng như vậy một cách dễ dàng.

Một hàm giống như realloc hoặc giống như tự do trước tiên kiểm tra xem các byte pymem_forbiddenbyte ở mỗi đầu có còn nguyên vẹn không. Nếu họ đã bị thay đổi, đầu ra chẩn đoán được ghi vào stderr và chương trình bị hủy bỏ thông qua py_fatalerror (). Chế độ thất bại chính khác đang gây ra lỗi bộ nhớ khi một chương trình đọc một trong các mẫu bit đặc biệt và cố gắng sử dụng nó làm địa chỉ. Nếu bạn nhận được một trình gỡ lỗi sau đó và nhìn vào đối tượng, bạn có thể thấy rằng nó hoàn toàn chứa đầy pymem_deadbyte (có nghĩa là bộ nhớ được giải phóng đang được sử dụng) hoặc pymem_cleanbyte (có nghĩa là bộ nhớ không được sử dụng).

Đã thay đổi trong phiên bản 3.6: Hàm

PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
39 hiện cũng hoạt động trên Python được biên dịch ở chế độ phát hành. Lỗi, các móc gỡ lỗi hiện sử dụng
PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
62 để có được dấu vết trong đó một khối bộ nhớ được phân bổ. Các móc gỡ lỗi bây giờ cũng kiểm tra xem GIL có được giữ khi các chức năng của các miền
PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
58 và
PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
60 được gọi không.The
PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
39 function now also works on Python compiled in release mode. On error, the debug hooks now use
PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
62 to get the traceback where a memory block was allocated. The debug hooks now also check if the GIL is held when functions of
PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
58 and
PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
60 domains are called.

Thay đổi trong phiên bản 3.8: Các mẫu byte

PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
87 (
PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
51),
PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
89 (
PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
53) và
PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
91 (
PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
55) đã được thay thế bằng
PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
50,
PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
52 và
PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
54Byte patterns
PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
87 (
PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
51),
PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
89 (
PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
53) and
PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
91 (
PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
55) have been replaced with
PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
50,
PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
52 and
PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
54 to use the same values than Windows CRT debug malloc() and free().

Người phân bổ pymalloc

Python có bộ phân bổ pymalloc được tối ưu hóa cho các đối tượng nhỏ (nhỏ hơn hoặc bằng 512 byte) với tuổi thọ ngắn. Nó sử dụng các ánh xạ bộ nhớ được gọi là đấu trường trên mạng với kích thước cố định là 256 kib. Nó rơi trở lại

PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
1 và
char *buf1 = PyMem_New(char, BUFSIZ);
char *buf2 = (char *) malloc(BUFSIZ);
char *buf3 = (char *) PyMem_Malloc(BUFSIZ);
...
PyMem_Del(buf3);  /* Wrong -- should be PyMem_Free() */
free(buf2);       /* Right -- allocated via malloc() */
free(buf1);       /* Fatal -- should be PyMem_Del()  */
2 để phân bổ lớn hơn 512 byte.

Pymalloc là bộ phân bổ mặc định của các miền

PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
60 (Xh:
PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
4) và
PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
58 (ví dụ:
PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
2).default allocator of the
PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
60 (ex:
PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
4) and
PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
58 (ex:
PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
2) domains.

Trình phân bổ đấu trường sử dụng các chức năng sau:

  • PyObject *res;
    char *buf = PyMem_New(char, BUFSIZ); /* for I/O */
    
    if (buf == NULL)
        return PyErr_NoMemory();
    /* ...Do some I/O operation involving buf... */
    res = PyBytes_FromString(buf);
    PyMem_Del(buf); /* allocated with PyMem_New */
    return res;
    
    04 và
    PyObject *res;
    char *buf = PyMem_New(char, BUFSIZ); /* for I/O */
    
    if (buf == NULL)
        return PyErr_NoMemory();
    /* ...Do some I/O operation involving buf... */
    res = PyBytes_FromString(buf);
    PyMem_Del(buf); /* allocated with PyMem_New */
    return res;
    
    05 trên Windows,

  • PyObject *res;
    char *buf = PyMem_New(char, BUFSIZ); /* for I/O */
    
    if (buf == NULL)
        return PyErr_NoMemory();
    /* ...Do some I/O operation involving buf... */
    res = PyBytes_FromString(buf);
    PyMem_Del(buf); /* allocated with PyMem_New */
    return res;
    
    06 và
    PyObject *res;
    char *buf = PyMem_New(char, BUFSIZ); /* for I/O */
    
    if (buf == NULL)
        return PyErr_NoMemory();
    /* ...Do some I/O operation involving buf... */
    res = PyBytes_FromString(buf);
    PyMem_Del(buf); /* allocated with PyMem_New */
    return res;
    
    07 nếu có,

  • malloc()free() khác.

Bộ phân bổ này bị vô hiệu hóa nếu Python được cấu hình với tùy chọn

PyObject *res;
char *buf = PyMem_New(char, BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Del(buf); /* allocated with PyMem_New */
return res;
10. Nó cũng có thể bị vô hiệu hóa trong thời gian chạy bằng biến môi trường PYTHONMALLOC (ví dụ:
PyObject *res;
char *buf = PyMem_New(char, BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Del(buf); /* allocated with PyMem_New */
return res;
12).PYTHONMALLOC environment variable (ex:
PyObject *res;
char *buf = PyMem_New(char, BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Del(buf); /* allocated with PyMem_New */
return res;
12).

Tùy chỉnh Phân bổ đấu trường Pymalloc

Mới trong phiên bản 3.4.

TypePyObjectArenAllocator công trình được sử dụng để mô tả một trình phân bổ đấu trường. Cấu trúc có ba trường:PyObjectArenaAllocator

Structure used to describe an arena allocator. The structure has three fields:

Đồng ruộng

Nghĩa

PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
17

Bối cảnh người dùng được thông qua như đối số đầu tiên

PyObject *res;
char *buf = PyMem_New(char, BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Del(buf); /* allocated with PyMem_New */
return res;
14

Phân bổ một đấu trường có kích thước byte

PyObject *res;
char *buf = PyMem_New(char, BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Del(buf); /* allocated with PyMem_New */
return res;
15

miễn phí một đấu trường

VoidPyObject_GetArenAallocator (PyObjectArenAallocator*Trình phân bổ)PyObject_GetArenaAllocator(PyObjectArenaAllocator *allocator)

Get the arena allocator.

VoidPyObject_setArenAallocator (PyObjectArenAallocator*Phân bổ) ¶ PyObject_SetArenaAllocator(PyObjectArenaAllocator*allocator)

Đặt trình phân bổ đấu trường.

tracemalloc c api¶

Mới trong phiên bản 3.7.

IntPyTracemalLoc_Track (unsignIntdomain, UIntPtr_TPTR, size_tsize) ¶Track một khối bộ nhớ được phân bổ trong mô -đun
PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
62.
PyTraceMalloc_Track(unsigned intdomain, uintptr_tptr, size_tsize)

Track an allocated memory block in the

PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
62 module.

Trả về

PyObject *res;
char *buf = PyMem_New(char, BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Del(buf); /* allocated with PyMem_New */
return res;
17 Khi thành công, trả lại
PyObject *res;
char *buf = PyMem_New(char, BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Del(buf); /* allocated with PyMem_New */
return res;
18 khi lỗi (không phân bổ bộ nhớ để lưu trữ dấu vết). Trả về
PyObject *res;
char *buf = PyMem_New(char, BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Del(buf); /* allocated with PyMem_New */
return res;
19 Nếu Tracemalloc bị vô hiệu hóa.

Nếu khối bộ nhớ đã được theo dõi, hãy cập nhật dấu vết hiện có.

intpyTracemalLoc_untrack (unsignIntdomain, UINTPTR_TPTR) ¶untrack Một khối bộ nhớ được phân bổ trong mô -đun
PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
62. Không làm gì nếu khối không được theo dõi.
PyTraceMalloc_Untrack(unsignedintdomain, uintptr_tptr)

Untrack an allocated memory block in the

PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;
62 module. Do nothing if the block was not tracked.

Trả về

PyObject *res;
char *buf = PyMem_New(char, BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Del(buf); /* allocated with PyMem_New */
return res;
19 Nếu Tracemalloc bị vô hiệu hóa, nếu không, hãy trả về
PyObject *res;
char *buf = PyMem_New(char, BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Del(buf); /* allocated with PyMem_New */
return res;
17.

Ví dụ;

Dưới đây là ví dụ từ phần tổng quan, viết lại để bộ đệm I/O được phân bổ từ đống python bằng cách sử dụng bộ chức năng đầu tiên:Overview, rewritten so that the I/O buffer is allocated from the Python heap by using the first function set:

PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;

Cùng một mã sử dụng chức năng định hướng loại:

PyObject *res;
char *buf = PyMem_New(char, BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Del(buf); /* allocated with PyMem_New */
return res;

Lưu ý rằng trong hai ví dụ ở trên, bộ đệm luôn được thao tác thông qua các hàm thuộc cùng một bộ. Thật vậy, cần phải sử dụng cùng một họ API bộ nhớ cho một khối bộ nhớ nhất định, do đó nguy cơ trộn các bộ phân phối khác nhau bị giảm xuống mức tối thiểu. Trình tự mã sau đây chứa hai lỗi, một trong số đó được dán nhãn là gây tử vong vì nó trộn hai bộ phân bổ khác nhau hoạt động trên các đống khác nhau.

char *buf1 = PyMem_New(char, BUFSIZ);
char *buf2 = (char *) malloc(BUFSIZ);
char *buf3 = (char *) PyMem_Malloc(BUFSIZ);
...
PyMem_Del(buf3);  /* Wrong -- should be PyMem_Free() */
free(buf2);       /* Right -- allocated via malloc() */
free(buf1);       /* Fatal -- should be PyMem_Del()  */

Ngoài các chức năng nhằm xử lý các khối bộ nhớ thô từ đống Python, các đối tượng trong Python được phân bổ và phát hành với

PyObject *res;
char *buf = PyMem_New(char, BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Del(buf); /* allocated with PyMem_New */
return res;
23,
PyObject *res;
char *buf = PyMem_New(char, BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Del(buf); /* allocated with PyMem_New */
return res;
24 và
PyObject *res;
char *buf = PyMem_New(char, BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Del(buf); /* allocated with PyMem_New */
return res;
25.

Chúng sẽ được giải thích trong chương tiếp theo về việc xác định và thực hiện các loại đối tượng mới trong C.

Ví dụ bộ nhớ tĩnh là gì?

Ví dụ, khai báo mảng là một ví dụ về phân bổ bộ nhớ tĩnh và kích thước của mảng phải được biết trước.Bạn không thể thay đổi kích thước của mảng một khi bộ nhớ được phân bổ.the array declaration is an example of a static memory allocation and the size of the array must be known beforehand. You cannot change the size of the array once the memory is allocated.

Bộ nhớ tĩnh trong lập trình là gì?

1. Bộ nhớ tĩnh, trong đó một đối tượng được phân bổ bởi trình liên kết trong suốt thời gian của chương trình.Các biến toàn cầu, thành viên lớp tĩnh và các biến tĩnh trong các hàm được phân bổ trong bộ nhớ tĩnh.Một đối tượng được phân bổ trong bộ nhớ tĩnh được xây dựng một lần và tồn tại đến cuối chương trình.in which an object is allocated by the linker for the duration of the program. Global variables, static class members, and static variables in functions are allocated in static memory. An object allocated in static memory is constructed once and persists to the end of the program.

Loại bộ nhớ trong Python là gì?

Phân bổ bộ nhớ Python Trình quản lý bộ nhớ Python có trách nhiệm quản lý phân bổ bộ nhớ và phân phối cho các quy trình khác nhau đang được thực hiện trong Python.Mỗi quá trình Python có hai loại bộ nhớ được phân bổ cho nó.Bộ nhớ tĩnh.Bộ nhớ động.Static Memory. Dynamic Memory.

Bộ nhớ tĩnh và bộ nhớ động với ví dụ là gì?

Sự khác biệt giữa phân bổ bộ nhớ tĩnh và động.