- unstable 4.31.0-1
| malloc_info(3) | Library Functions Manual | malloc_info(3) |
الاسم¶
malloc_info - تصدير حالة malloc إلى دفق
المكتبة¶
مكتبة سي المعيارية (libc، -lc)
موجز¶
#include <malloc.h>
int malloc_info(int options, FILE *stream);
الوصف¶
تصدّر الدالة malloc_info() سلسلة XML تصف الحالة الحالية لتنفيذ تخصيص الذاكرة في المستدعي. تُطبع السلسلة على دفق الملف stream. تتضمن السلسلة المُصدّرة معلومات عن جميع الساحات (انظر malloc(3)).
كما هو مطبّق حالياً، يجب أن يكون options صفراً.
قيمة الإرجاع¶
عند النجاح، تُعيد malloc_info() 0. عند الفشل، تُعيد -1، ويُضبط errno للإشارة إلى الخطأ.
الأخطاء¶
- EINVAL
- options كان غير صفري.
السمات¶
للاطلاع على شرح للمصطلحات المستخدمة في هذا القسم، انظر attributes(7).
| الواجهة | السمة | القيمة |
| malloc_info() | سلامة الخيوط | MT-Safe |
المعايير¶
GNU.
التاريخ¶
glibc 2.10.
ملاحظات¶
تُقدّم معلومات تخصيص الذاكرة كسلسلة XML (بدلاً من بنية C) لأن المعلومات قد تتغير بمرور الوقت (تبعاً للتغييرات في التنفيذ الأساسي). تتضمن سلسلة XML الناتجة حقل إصدار.
يمكن استخدام الدالة open_memstream(3) لإرسال مخرجات malloc_info() مباشرةً إلى مخزن مؤقت في الذاكرة، بدلاً من إرسالها إلى ملف.
صُمّمت الدالة malloc_info() لمعالجة أوجه القصور في malloc_stats(3) و mallinfo(3).
أمثلة¶
يقبل البرنامج أدناه ما يصل إلى أربع وسائط لسطر الأوامر، أول ثلاث منها إلزامية. تُحدد الوسيطة الأولى عدد الخيوط التي يجب أن ينشئها البرنامج. تُخصّص جميع الخيوط، بما في ذلك الخيط الرئيس، عدد الكتل الذاكرية المُحدّد بالوسيطة الثانية. تتحكم الوسيطة الثالثة في حجم الكتل التي سيتم تخصيصها. ينشئ الخيط الرئيس كتلًا بهذا الحجم، ويُخصّص الخيط الثاني الذي أنشأه البرنامج كتلًا بحجم مضاعف، ويُخصّص الخيط الثالث كتلًا بحجم ثلاثي، وهكذا.
يستدعي البرنامج malloc_info() مرتين لعرض حالة تخصيص الذاكرة. يحدث الاستدعاء الأول قبل إنشاء أي خيوط أو تخصيص ذاكرة. يُنفّذ الاستدعاء الثاني بعد أن تُخصّص جميع الخيوط الذاكرة.
في المثال التالي، تُحدد وسائط سطر الأوامر إنشاء خيط إضافي واحد، ويُخصّص كل من الخيط الرئيس والخيط الإضافي 10000 كتلة ذاكرة. بعد تخصيص كتل الذاكرة، يُظهر malloc_info() حالة ساحتي تخصيص.
$ getconf GNU_LIBC_VERSION glibc 2.13 $ ./a.out 1 10000 100 ============ Before allocating blocks ============ <malloc version="1"> <heap nr="0"> <sizes> </sizes> <total type="fast" count="0" size="0"/> <total type="rest" count="0" size="0"/> <system type="current" size="135168"/> <system type="max" size="135168"/> <aspace type="total" size="135168"/> <aspace type="mprotect" size="135168"/> </heap> <total type="fast" count="0" size="0"/> <total type="rest" count="0" size="0"/> <system type="current" size="135168"/> <system type="max" size="135168"/> <aspace type="total" size="135168"/> <aspace type="mprotect" size="135168"/> </malloc> ============ After allocating blocks ============ <malloc version="1"> <heap nr="0"> <sizes> </sizes> <total type="fast" count="0" size="0"/> <total type="rest" count="0" size="0"/> <system type="current" size="1081344"/> <system type="max" size="1081344"/> <aspace type="total" size="1081344"/> <aspace type="mprotect" size="1081344"/> </heap> <heap nr="1"> <sizes> </sizes> <total type="fast" count="0" size="0"/> <total type="rest" count="0" size="0"/> <system type="current" size="1032192"/> <system type="max" size="1032192"/> <aspace type="total" size="1032192"/> <aspace type="mprotect" size="1032192"/> </heap> <total type="fast" count="0" size="0"/> <total type="rest" count="0" size="0"/> <system type="current" size="2113536"/> <system type="max" size="2113536"/> <aspace type="total" size="2113536"/> <aspace type="mprotect" size="2113536"/> </malloc>
مصدر البرنامج¶
#include <err.h>
#include <errno.h>
#include <malloc.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
static size_t blockSize;
static size_t numThreads;
static unsigned int numBlocks;
static void *
thread_func(void *arg)
{
int tn = (int) arg;
/* The multiplier '(2 + tn)' ensures that each thread (including
the main thread) allocates a different amount of memory. */
for (unsigned int j = 0; j < numBlocks; j++)
if (malloc(blockSize * (2 + tn)) == NULL)
err(EXIT_FAILURE, "malloc-thread");
sleep(100); /* Sleep until main thread terminates. */
return NULL;
}
int
main(int argc, char *argv[])
{
int sleepTime;
pthread_t *thr;
if (argc < 4) {
fprintf(stderr,
"%s num-threads num-blocks block-size [sleep-time]\n",
argv[0]);
exit(EXIT_FAILURE);
}
numThreads = atoi(argv[1]);
numBlocks = atoi(argv[2]);
blockSize = atoi(argv[3]);
sleepTime = (argc > 4) ? atoi(argv[4]) : 0;
thr = calloc(numThreads, sizeof(*thr));
if (thr == NULL)
err(EXIT_FAILURE, "calloc");
printf("============ Before allocating blocks ============\n");
malloc_info(0, stdout);
/* Create threads that allocate different amounts of memory. */
for (size_t tn = 0; tn < numThreads; tn++) {
errno = pthread_create(&thr[tn], NULL, thread_func,
(void *) tn);
if (errno != 0)
err(EXIT_FAILURE, "pthread_create");
/* If we add a sleep interval after the start-up of each
thread, the threads likely won't contend for malloc
mutexes, and therefore additional arenas won't be
allocated (see malloc(3)). */
if (sleepTime > 0)
sleep(sleepTime);
}
/* The main thread also allocates some memory. */
for (unsigned int j = 0; j < numBlocks; j++)
if (malloc(blockSize) == NULL)
err(EXIT_FAILURE, "malloc");
sleep(2); /* Give all threads a chance to
complete allocations. */
printf("\n============ After allocating blocks ============\n");
malloc_info(0, stdout);
exit(EXIT_SUCCESS);
}
انظر أيضًا¶
mallinfo(3), malloc(3), malloc_stats(3), mallopt(3), open_memstream(3)
ترجمة¶
تُرجمت هذه الصفحة من الدليل بواسطة زايد السعيدي <zayed.alsaidi@gmail.com>
هذه الترجمة هي وثيقة مجانية؛ راجع رخصة جنو العامة الإصدار 3 أو ما بعده للاطلاع على شروط حقوق النشر. لا توجد أي ضمانات.
إذا وجدت أي أخطاء في ترجمة صفحة الدليل هذه، يرجى إرسال بريد إلكتروني إلى قائمة بريد المترجمين: kde-l10n-ar@kde.org.
| 8 فبراير 2026 | صفحات دليل لينكس 6.18 |