有什么理由在PyMem_Malloc上使用malloc吗? [英] Is there any reason to use malloc over PyMem_Malloc?

查看:313
本文介绍了有什么理由在PyMem_Malloc上使用malloc吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读有关Python C中内存管理的文档扩展,据我所知,似乎没有太多理由使用malloc而不是PyMem_Malloc.假设我想分配一个不会暴露给Python源代码的数组,该数组将存储在将被垃圾回收的对象中.有什么理由要使用malloc?

I'm reading the documentation for Memory Management in Python C extensions, and as far as I can tell, there doesn't really seem to be much reason to use malloc rather than PyMem_Malloc. Say I want to allocate an array that isn't to be exposed to Python source code and will be stored in an object that will be garbage collected. Is there any reason to use malloc?

推荐答案

EDIT :混合的PyMem_MallocPyObject_Malloc更正;他们是两个不同的电话.

EDIT: Mixed PyMem_Malloc and PyObject_Malloc corrections; they are two different calls.

在未激活PYMALLOC_DEBUG宏的情况下,PyMem_Malloc是libc的malloc()的别名,有一种特殊情况:调用PyMem_Malloc分配零字节将返回非NULL指针,而malloc(zero_bytes)可能返回NULL值或引发系统错误(源代码参考):

Without the PYMALLOC_DEBUG macro activated, PyMem_Malloc is an alias of libc's malloc(), having one special case: calling PyMem_Malloc to allocate zero bytes will return a non-NULL pointer, while malloc(zero_bytes) might return a NULL value or raise a system error (source code reference):

/* malloc.请注意,nbytes == 0尝试 返回一个非NULL指针,不同 *来自所有其他当前活动的指针.这可能是不可能的. */

/* malloc. Note that nbytes==0 tries to return a non-NULL pointer, distinct * from all other currently live pointers. This may not be possible. */

此外,在 pymem.h头文件:

Also, there is an advisory note on the pymem.h header file:

永远不要将对PyMem_的调用与 调用平台malloc/realloc/ calloc/免费.例如,在Windows上 不同的DLL可能最终会使用 不同的堆,如果使用 PyMem_Malloc您将获得内存 从Python使用的堆中 DLL;如果你可能会是一场灾难 free()直接由您自己创建 扩大.改用PyMem_Free 确保Python可以返回 内存到适当的堆.作为另一个 例如,在PYMALLOC_DEBUG模式下, Python将所有调用包装到所有PyMem_ 和PyObject_记忆功能 添加的特殊调试包装 其他调试信息 动态内存块.系统 例程不知道该怎么办 这些东西,还有Python 包装者不知道该怎么办 通过直接获得原始块 然后是系统例程.

Never mix calls to PyMem_ with calls to the platform malloc/realloc/ calloc/free. For example, on Windows different DLLs may end up using different heaps, and if you use PyMem_Malloc you'll get the memory from the heap used by the Python DLL; it could be a disaster if you free()'ed that directly in your own extension. Using PyMem_Free instead ensures Python can return the memory to the proper heap. As another example, in PYMALLOC_DEBUG mode, Python wraps all calls to all PyMem_ and PyObject_ memory functions in special debugging wrappers that add additional debugging info to dynamic memory blocks. The system routines have no idea what to do with that stuff, and the Python wrappers have no idea what to do with raw blocks obtained directly by the system routines then.

然后, PyMem_Malloc PyObject_Malloc内部有一些特定于Python的调优,该函数不仅用于C扩展,还用于运行Python程序(例如100*234)时的所有动态分配. ,str(100)10 + 4j:

Then, there are some Python specific tunings inside PyMem_Malloc PyObject_Malloc, a function used not only for C extensions but for all the dynamic allocations while running a Python program, like 100*234, str(100) or 10 + 4j:

>>> id(10 + 4j)
139721697591440
>>> id(10 + 4j)
139721697591504
>>> id(10 + 4j)
139721697591440

以前的complex()实例是在专用池上分配的小对象.

The previous complex() instances are small objects allocated on a dedicated pool.

使用 PyMem_Malloc PyObject_Malloc分配小对象(<256字节)非常有效,因为它是由8个字节对齐的块的池完成的,每个块大小都有一个池.还有Pages和Arenas块可用于更大的分配.

Small objects (<256 bytes) allocation with PyMem_Malloc PyObject_Malloc is quite efficient since it's done from a pool 8 bytes aligned blocks, existing one pool for each block size. There are also Pages and Arenas blocks for bigger allocations.

源代码的评论解释了如何优化PyObject_Malloc调用:

This comment on the source code explains how the PyObject_Malloc call is optimized:

/*
 * The basic blocks are ordered by decreasing execution frequency,
 * which minimizes the number of jumps in the most common cases,
 * improves branching prediction and instruction scheduling (small
 * block allocations typically result in a couple of instructions).
 * Unless the optimizer reorders everything, being too smart...
 */

池,页面和Arenas是旨在减少外部内存碎片长期运行的Python程序.

Pools, Pages and Arenas are optimizations intended to reduce external memory fragmentation of long running Python programs.

查看源代码以获得完整的详细文档在Python的内存内部.

Check out the source code for the full detailed documentation on Python's memory internals.

这篇关于有什么理由在PyMem_Malloc上使用malloc吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆