Malloc impl

Implements Malloc.

Summary
Malloc implImplements Malloc.
CopyrightThis program is free software.
Files
C-kern/api/platform/malloc.hHeader file of Malloc.
C-kern/platform/Linux/malloc.cImplementation file of Malloc impl.
malloc
static variables
s_isprepared_mallocRemembers if prepare_malloc was called already.
init
prepare_mallocCalls functions to force allocating of system memory.
manage
trimmemory_mallocUses GNU malloc_trim extension.
query
allocatedsize_malloc implUses GNU malloc_stats extension.
test

Copyright

This program is free software.  You can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

Author

© 2011 Jörg Seebohn

Files

C-kern/api/platform/malloc.h

Header file of Malloc.

C-kern/platform/Linux/malloc.c

Implementation file of Malloc impl.

malloc

Summary
static variables
s_isprepared_mallocRemembers if prepare_malloc was called already.
init
prepare_mallocCalls functions to force allocating of system memory.
manage
trimmemory_mallocUses GNU malloc_trim extension.
query
allocatedsize_malloc implUses GNU malloc_stats extension.
test

static variables

s_isprepared_malloc

static bool s_isprepared_malloc

Remembers if prepare_malloc was called already.

init

prepare_malloc

int prepare_malloc()

Calls functions to force allocating of system memory.  No own malloc version is initialized: See allocatedsize_malloc for why.

manage

trimmemory_malloc

int trimmemory_malloc()

Uses GNU malloc_trim extension.  This function may be missing on some platforms.  Currently it is only working on Linux platforms.

query

allocatedsize_malloc impl

Uses GNU malloc_stats extension.  This functions returns internal collected statistics about memory usage so implementing a thin wrapper for malloc is not necessary.  This function may be missing on some platforms.  Currently it is only tested on Linux platforms.

What malloc_stats does

The GNU C lib function malloc_stats writes textual information to standard err in the following form:

Arena 0:
system bytes     =     135168
in use bytes     =      15000
Total (incl. mmap):
system bytes     =     135168
in use bytes     =      15000
max mmap regions =          0
max mmap bytes   =          0

How it is implemented

This function redirects standard error file descriptor to a pipe and reads the content of the pipe into a buffer.  It scans backwards until the third last line is reached (“in use bytes”) and then returns the converted the number at the end of the line as result.

test

Offers an interface to check for system memory allocated with malloc and co.
Implements Malloc.
static bool s_isprepared_malloc
Remembers if prepare_malloc was called already.
int prepare_malloc()
Calls functions to force allocating of system memory.
int trimmemory_malloc()
Uses GNU malloc_trim extension.
int allocatedsize_malloc(/*out*/size_t *number_of_allocated_bytes)
Returns allocated number of bytes which are not freed.
Close