|
21 | 21 | #include <stdint.h> |
22 | 22 | #include <errno.h> |
23 | 23 | #include <unistd.h> |
| 24 | +#include <malloc.h> |
24 | 25 |
|
25 | 26 | /* |
26 | 27 | sbrk |
@@ -48,3 +49,79 @@ caddr_t _sbrk(int incr) |
48 | 49 |
|
49 | 50 | return (caddr_t)prev_heap_end; |
50 | 51 | } |
| 52 | + |
| 53 | +// struct mallinfo { |
| 54 | +// size_t arena; /* total space allocated from system */ |
| 55 | +// size_t ordblks; /* number of non-inuse chunks */ |
| 56 | +// size_t smblks; /* unused -- always zero */ |
| 57 | +// size_t hblks; /* number of mmapped regions */ |
| 58 | +// size_t hblkhd; /* total space in mmapped regions */ |
| 59 | +// size_t usmblks; /* unused -- always zero */ |
| 60 | +// size_t fsmblks; /* unused -- always zero */ |
| 61 | +// size_t uordblks; /* total allocated space */ |
| 62 | +// size_t fordblks; /* total non-inuse space */ |
| 63 | +// size_t keepcost; /* top-most, releasable (via malloc_trim) space */ |
| 64 | +// }; |
| 65 | + |
| 66 | +/* |
| 67 | +The structure fields contain the following information: |
| 68 | +
|
| 69 | + arena The total amount of memory allocated by means other than |
| 70 | + mmap(2) (i.e., memory allocated on the heap). This figure |
| 71 | + includes both in-use blocks and blocks on the free list. |
| 72 | +
|
| 73 | + ordblks |
| 74 | + The number of ordinary (i.e., non-fastbin) free blocks. |
| 75 | +
|
| 76 | + smblks The number of fastbin free blocks (see mallopt(3)). |
| 77 | +
|
| 78 | + hblks The number of blocks currently allocated using mmap(2). |
| 79 | + (See the discussion of M_MMAP_THRESHOLD in mallopt(3).) |
| 80 | +
|
| 81 | + hblkhd The number of bytes in blocks currently allocated using |
| 82 | + mmap(2). |
| 83 | +
|
| 84 | + usmblks |
| 85 | + This field is unused, and is always 0. Historically, it |
| 86 | + was the "highwater mark" for allocated space—that is, the |
| 87 | + maximum amount of space that was ever allocated (in |
| 88 | + bytes); this field was maintained only in nonthreading |
| 89 | + environments. |
| 90 | +
|
| 91 | + fsmblks |
| 92 | + The total number of bytes in fastbin free blocks. |
| 93 | +
|
| 94 | + uordblks |
| 95 | + The total number of bytes used by in-use allocations. |
| 96 | +
|
| 97 | + fordblks |
| 98 | + The total number of bytes in free blocks. |
| 99 | +
|
| 100 | + keepcost |
| 101 | + The total amount of releasable free space at the top of |
| 102 | + the heap. This is the maximum number of bytes that could |
| 103 | + ideally (i.e., ignoring page alignment restrictions, and |
| 104 | + so on) be released by malloc_trim(3). |
| 105 | +*/ |
| 106 | + |
| 107 | +struct mallinfo mallinfo(void) |
| 108 | +{ |
| 109 | + struct mallinfo temp_mallinfo; |
| 110 | + |
| 111 | + if (heap_end == 0) { |
| 112 | + heap_end = (caddr_t)&__HeapBase; |
| 113 | + } |
| 114 | + |
| 115 | + temp_mallinfo.arena = ((size_t)&__HeapLimit - (size_t)&__HeapBase); |
| 116 | + temp_mallinfo.ordblks = 0; /* Unused */ |
| 117 | + temp_mallinfo.smblks = 0; /* Unused */ |
| 118 | + temp_mallinfo.hblks = 0; /* Unused */ |
| 119 | + temp_mallinfo.hblkhd = 0; /* Unused */ |
| 120 | + temp_mallinfo.usmblks = 0; /* Unused */ |
| 121 | + temp_mallinfo.fsmblks = 0; /* Unused */ |
| 122 | + temp_mallinfo.uordblks = (size_t)heap_end - (size_t)&__HeapBase; |
| 123 | + temp_mallinfo.fordblks = (size_t)&__HeapLimit - (size_t)heap_end; |
| 124 | + temp_mallinfo.keepcost = 0 /* Unused */; |
| 125 | + |
| 126 | + return temp_mallinfo; |
| 127 | +} |
0 commit comments