summaryrefslogtreecommitdiffstats
path: root/cpukit/libcsupport/src/malloc_initialize.c
diff options
context:
space:
mode:
authorJoel Sherrill <joel.sherrill@OARcorp.com>2007-12-18 20:36:40 +0000
committerJoel Sherrill <joel.sherrill@OARcorp.com>2007-12-18 20:36:40 +0000
commit543fe820616f31350366ab61052050303d17dd25 (patch)
treea2e4702be2cd9988ed341586c1c8c976b8ba3bb8 /cpukit/libcsupport/src/malloc_initialize.c
parent2007-12-18 Joel Sherrill <joel.sherrill@OARcorp.com> (diff)
downloadrtems-543fe820616f31350366ab61052050303d17dd25.tar.bz2
2007-12-18 Joel Sherrill <joel.sherrill@oarcorp.com>
* libcsupport/Makefile.am, libcsupport/preinstall.am, libcsupport/src/malloc.c, libcsupport/src/mallocinfo.c, libmisc/Makefile.am, libmisc/shell/main_mallocinfo.c, libmisc/shell/shellconfig.h: Split malloc.c into multiple files with one function per file. Also split out statistics into a separate file which can be plugged in dynamically. Right now, it is always in. I suspect that splitting the file removed more code than leaving statistics in. I tinkered with malloc information command in the shell. I resurrected the malloc arena code as malloc boundary. This code is now compiled all the time even though it does not appear to work. * libcsupport/include/rtems/malloc.h, libcsupport/src/_calloc_r.c, libcsupport/src/_free_r.c, libcsupport/src/_malloc_r.c, libcsupport/src/_realloc_r.c, libcsupport/src/calloc.c, libcsupport/src/free.c, libcsupport/src/malloc_boundary.c, libcsupport/src/malloc_get_statistics.c, libcsupport/src/malloc_initialize.c, libcsupport/src/malloc_p.h, libcsupport/src/malloc_report_statistics.c, libcsupport/src/malloc_report_statistics_plugin.c, libcsupport/src/malloc_statistics_helpers.c, libcsupport/src/malloc_walk.c, libcsupport/src/realloc.c, libmisc/shell/main_perioduse.c: New files.
Diffstat (limited to 'cpukit/libcsupport/src/malloc_initialize.c')
-rw-r--r--cpukit/libcsupport/src/malloc_initialize.c128
1 files changed, 128 insertions, 0 deletions
diff --git a/cpukit/libcsupport/src/malloc_initialize.c b/cpukit/libcsupport/src/malloc_initialize.c
new file mode 100644
index 0000000000..08fc351f7f
--- /dev/null
+++ b/cpukit/libcsupport/src/malloc_initialize.c
@@ -0,0 +1,128 @@
+/*
+ * RTEMS Malloc Family Implementation --Initialization
+ *
+ *
+ * COPYRIGHT (c) 1989-2007.
+ * On-Line Applications Research Corporation (OAR).
+ *
+ * The license and distribution terms for this file may be
+ * found in the file LICENSE in this distribution or at
+ * http://www.rtems.com/license/LICENSE.
+ *
+ * $Id$
+ */
+
+#if HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <rtems.h>
+#include <rtems/malloc.h>
+#include "malloc_p.h"
+
+Heap_Control RTEMS_Malloc_Heap;
+Chain_Control RTEMS_Malloc_GC_list;
+size_t RTEMS_Malloc_Sbrk_amount;
+rtems_malloc_statistics_t rtems_malloc_statistics;
+
+void RTEMS_Malloc_Initialize(
+ void *start,
+ size_t length,
+ size_t sbrk_amount
+)
+{
+ uint32_t status;
+ void *starting_address;
+ uintptr_t old_address;
+ uintptr_t uaddress;
+
+ #if defined(RTEMS_MALLOC_BOUNDARY_HELPERS)
+ /*
+ * If configured, initialize the boundary support
+ */
+ if (rtems_malloc_boundary_helpers)
+ (*rtems_malloc_boundary_helpers->initialize)();
+ #endif
+
+ /*
+ * If configured, initialize the statistics support
+ */
+ if ( rtems_malloc_statistics_helpers )
+ (*rtems_malloc_statistics_helpers->initialize)();
+
+ /*
+ * Initialize the garbage collection list to start with nothing on it.
+ */
+ Chain_Initialize_empty(&RTEMS_Malloc_GC_list);
+
+ /*
+ * If the starting address is 0 then we are to attempt to
+ * get length worth of memory using sbrk. Make sure we
+ * align the address that we get back.
+ */
+
+ starting_address = start;
+ RTEMS_Malloc_Sbrk_amount = sbrk_amount;
+
+ if (!starting_address) {
+ uaddress = (uintptr_t)sbrk(length);
+
+ if (uaddress == (uintptr_t) -1) {
+ rtems_fatal_error_occurred( RTEMS_NO_MEMORY );
+ /* DOES NOT RETURN!!! */
+ }
+
+ if (uaddress & (CPU_HEAP_ALIGNMENT-1)) {
+ old_address = uaddress;
+ uaddress = (uaddress + CPU_HEAP_ALIGNMENT) & ~(CPU_HEAP_ALIGNMENT-1);
+
+ /*
+ * adjust the length by whatever we aligned by
+ */
+ length -= uaddress - old_address;
+ }
+
+ starting_address = (void *)uaddress;
+ }
+
+ /*
+ * If the BSP is not clearing out the workspace, then it is most likely
+ * not clearing out the initial memory for the heap. There is no
+ * standard supporting zeroing out the heap memory. But much code
+ * with UNIX history seems to assume that memory malloc'ed during
+ * initialization (before any free's) is zero'ed. This is true most
+ * of the time under UNIX because zero'ing memory when it is first
+ * given to a process eliminates the chance of a process seeing data
+ * left over from another process. This would be a security violation.
+ */
+
+ if ( rtems_configuration_get_do_zero_of_workspace() )
+ memset( starting_address, 0, length );
+
+ /*
+ * Unfortunately we cannot use assert if this fails because if this
+ * has failed we do not have a heap and if we do not have a heap
+ * STDIO cannot work because there will be no buffers.
+ */
+
+ status = _Protected_heap_Initialize(
+ &RTEMS_Malloc_Heap,
+ starting_address,
+ length,
+ CPU_HEAP_ALIGNMENT
+ );
+ if ( !status )
+ rtems_fatal_error_occurred( status );
+
+ #if defined(RTEMS_HEAP_DEBUG)
+ if ( _Protected_heap_Walk( &RTEMS_Malloc_Heap, 0, FALSE ) ) {
+ printk( "Malloc heap not initialized correctly\n" );
+ rtems_print_buffer( start, 32 );
+ printk( "\n" );
+ rtems_print_buffer( (start + length) - 48, 48 );
+ rtems_fatal_error_occurred( RTEMS_NO_MEMORY );
+ }
+ #endif
+
+ MSBUMP(space_available, length);
+}