source: rtems/c/src/lib/libc/malloc.c @ 1f2d5df

4.104.114.84.95
Last change on this file since 1f2d5df was 1f2d5df, checked in by Joel Sherrill <joel.sherrill@…>, on 09/18/96 at 20:52:47

added malloc_walk

miscellaneous corrections in casting.

  • Property mode set to 100644
File size: 8.4 KB
Line 
1/*
2 *  RTEMS Malloc Family Implementation
3 *
4 *
5 *  COPYRIGHT (c) 1989, 1990, 1991, 1992, 1993, 1994.
6 *  On-Line Applications Research Corporation (OAR).
7 *  All rights assigned to U.S. Government, 1994.
8 *
9 *  This material may be reproduced by or for the U.S. Government pursuant
10 *  to the copyright license under the clause at DFARS 252.227-7013.  This
11 *  notice must appear in all copies of this file and its derivatives.
12 *
13 *  $Id$
14 */
15
16#define __RTEMS_VIOLATE_KERNEL_VISIBILITY__
17#include <rtems.h>
18#include "libcsupport.h"
19#ifdef RTEMS_NEWLIB
20#include <sys/reent.h>
21#endif
22
23#include <stdio.h>
24#include <stdlib.h>
25#include <sys/types.h>
26#include <assert.h>
27#include <errno.h>
28#include <string.h>
29#include <unistd.h>    /* sbrk(2) */
30
31rtems_id RTEMS_Malloc_Heap;
32size_t RTEMS_Malloc_Sbrk_amount;
33
34#ifdef RTEMS_DEBUG
35#define MALLOC_STATS
36#define MALLOC_DIRTY
37#endif
38
39#ifdef MALLOC_STATS
40#define MSBUMP(f,n)    malloc_stats.f += (n)
41
42struct {
43    unsigned32  space_available;             /* current size of malloc area */
44    unsigned32  malloc_calls;                /* # calls to malloc */
45    unsigned32  free_calls;
46    unsigned32  realloc_calls;
47    unsigned32  calloc_calls;
48    unsigned32  max_depth;                   /* most ever malloc'd at 1 time */
49    unsigned64  lifetime_allocated;
50    unsigned64  lifetime_freed;
51} malloc_stats;
52
53#else                   /* No malloc_stats */
54#define MSBUMP(f,n)
55#endif
56
57void RTEMS_Malloc_Initialize(
58  void   *start,
59  size_t  length,
60  size_t  sbrk_amount
61)
62{
63  rtems_status_code   status;
64  void               *starting_address;
65  rtems_unsigned32    old_address;
66  rtems_unsigned32    u32_address;
67
68  /*
69   * If the starting address is 0 then we are to attempt to
70   * get length worth of memory using sbrk. Make sure we
71   * align the address that we get back.
72   */
73
74  starting_address = start;
75  RTEMS_Malloc_Sbrk_amount = sbrk_amount;
76
77  if (!starting_address) {
78    u32_address = (unsigned int)sbrk(length);
79
80    if (u32_address == -1) {
81      rtems_fatal_error_occurred( RTEMS_NO_MEMORY );
82      /* DOES NOT RETURN!!! */
83    }
84
85    if (u32_address & (CPU_ALIGNMENT-1)) {
86      old_address = u32_address;
87      u32_address = (u32_address + CPU_ALIGNMENT) & ~(CPU_ALIGNMENT-1);
88
89       /*
90        * adjust the length by whatever we aligned by
91        */
92
93      length -= u32_address - old_address;
94    }
95
96    starting_address = (void *)u32_address;
97  }
98
99  /*
100   *  Unfortunately we cannot use assert if this fails because if this
101   *  has failed we do not have a heap and if we do not have a heap
102   *  STDIO cannot work because there will be no buffers.
103   */
104
105  status = rtems_region_create(
106    rtems_build_name( 'H', 'E', 'A', 'P' ),
107    starting_address,
108    length,
109    CPU_ALIGNMENT,
110    RTEMS_DEFAULT_ATTRIBUTES,
111    &RTEMS_Malloc_Heap
112  );
113  if ( status != RTEMS_SUCCESSFUL )
114    rtems_fatal_error_occurred( status );
115
116#ifdef MALLOC_STATS
117  /* zero all the stats */
118  (void) memset(&malloc_stats, 0, sizeof(malloc_stats));
119#endif
120 
121  MSBUMP(space_available, length);
122}
123
124#ifdef RTEMS_NEWLIB
125void *malloc(
126  size_t  size
127)
128{
129  void              *return_this;
130  void              *starting_address;
131  rtems_unsigned32   the_size;
132  rtems_unsigned32   sbrk_amount;
133  rtems_status_code  status;
134
135  MSBUMP(malloc_calls, 1);
136
137  if ( !size )
138    return (void *) 0;
139
140  /*
141   * Try to give a segment in the current region if there is not
142   * enough space then try to grow the region using rtems_region_extend().
143   * If this fails then return a NULL pointer.
144   */
145
146  status = rtems_region_get_segment(
147    RTEMS_Malloc_Heap,
148    size,
149    RTEMS_NO_WAIT,
150    RTEMS_NO_TIMEOUT,
151    &return_this
152  );
153
154  if ( status != RTEMS_SUCCESSFUL ) {
155    /*
156     *  Round to the "requested sbrk amount" so hopefully we won't have
157     *  to grow again for a while.  This effectively does sbrk() calls
158     *  in "page" amounts.
159     */
160
161    sbrk_amount = RTEMS_Malloc_Sbrk_amount;
162
163    if ( sbrk_amount == 0 )
164      return (void *) 0;
165
166    the_size = ((size + sbrk_amount) / sbrk_amount * sbrk_amount);
167
168    if (((rtems_unsigned32)starting_address = (void *)sbrk(the_size)) == -1)
169      return (void *) 0;
170
171    status = rtems_region_extend(
172      RTEMS_Malloc_Heap,
173      starting_address,
174      the_size
175    );
176    if ( status != RTEMS_SUCCESSFUL ) {
177      sbrk(-the_size);
178      errno = ENOMEM;
179      return (void *) 0;
180    }
181   
182    MSBUMP(space_available, the_size);
183
184    status = rtems_region_get_segment(
185      RTEMS_Malloc_Heap,
186       size,
187       RTEMS_NO_WAIT,
188       RTEMS_NO_TIMEOUT,
189       &return_this
190    );
191    if ( status != RTEMS_SUCCESSFUL ) {
192      errno = ENOMEM;
193      return (void *) 0;
194    }
195  }
196
197#ifdef MALLOC_STATS
198  if (return_this)
199  {
200      unsigned32 actual_size;
201      unsigned32 current_depth;
202      status = rtems_region_get_segment_size(RTEMS_Malloc_Heap, return_this, &actual_size);
203      MSBUMP(lifetime_allocated, actual_size);
204      current_depth = malloc_stats.lifetime_allocated - malloc_stats.lifetime_freed;
205      if (current_depth > malloc_stats.max_depth)
206          malloc_stats.max_depth = current_depth;
207  }
208#endif
209
210#ifdef MALLOC_DIRTY
211  (void) memset(return_this, 0xCF, size);
212#endif
213
214  return return_this;
215}
216
217void *calloc(
218  size_t nelem,
219  size_t elsize
220)
221{
222  register char *cptr;
223  int length;
224
225  MSBUMP(calloc_calls, 1);
226
227  length = nelem * elsize;
228  cptr = malloc( length );
229  if ( cptr )
230    memset( cptr, '\0', length );
231
232  MSBUMP(malloc_calls, -1);   /* subtract off the malloc */
233
234  return cptr;
235}
236
237void *realloc(
238  void *ptr,
239  size_t size
240)
241{
242  rtems_unsigned32  old_size;
243  rtems_status_code status;
244  char *new_area;
245
246  MSBUMP(realloc_calls, 1);
247
248  if ( !ptr )
249    return malloc( size );
250
251  if ( !size ) {
252    free( ptr );
253    return (void *) 0;
254  }
255
256  new_area = malloc( size );
257 
258  MSBUMP(malloc_calls, -1);   /* subtract off the malloc */
259
260  if ( !new_area ) {
261    free( ptr );
262    return (void *) 0;
263  }
264
265  status = rtems_region_get_segment_size( RTEMS_Malloc_Heap, ptr, &old_size );
266  if ( status != RTEMS_SUCCESSFUL ) {
267    errno = EINVAL;
268    return (void *) 0;
269  }
270
271  memcpy( new_area, ptr, (size < old_size) ? size : old_size );
272  free( ptr );
273
274  return new_area;
275
276}
277
278void free(
279  void *ptr
280)
281{
282  rtems_status_code status;
283
284  MSBUMP(free_calls, 1);
285
286  if ( !ptr )
287    return;
288
289#ifdef MALLOC_STATS
290  {
291      unsigned32        size;
292      status = rtems_region_get_segment_size( RTEMS_Malloc_Heap, ptr, &size );
293      if ( status == RTEMS_SUCCESSFUL ) {
294          MSBUMP(lifetime_freed, size);
295      }
296  }
297#endif
298 
299  status = rtems_region_return_segment( RTEMS_Malloc_Heap, ptr );
300  if ( status != RTEMS_SUCCESSFUL ) {
301    errno = EINVAL;
302    assert( 0 );
303  }
304}
305/* end if RTEMS_NEWLIB */
306#endif
307
308#ifdef MALLOC_STATS
309/*
310 * Dump the malloc statistics
311 * May be called via atexit()  (installable by our bsp) or
312 * at any time by user
313 */
314
315void malloc_dump(void)
316{
317    unsigned32 allocated = malloc_stats.lifetime_allocated - malloc_stats.lifetime_freed;
318
319    printf("Malloc stats\n");
320    printf("  avail:%uk  allocated:%uk (%d%%) max:%uk (%d%%) lifetime:%Luk freed:%Luk\n",
321           (unsigned int) malloc_stats.space_available / 1024,
322           (unsigned int) allocated / 1024,
323           /* avoid float! */
324           (allocated * 100) / malloc_stats.space_available,
325           (unsigned int) malloc_stats.max_depth / 1024,
326           (malloc_stats.max_depth * 100) / malloc_stats.space_available,
327           (unsigned64) malloc_stats.lifetime_allocated / 1024,
328           (unsigned64) malloc_stats.lifetime_freed / 1024);
329    printf("  Call counts:   malloc:%d   free:%d   realloc:%d   calloc:%d\n",
330           malloc_stats.malloc_calls,
331           malloc_stats.free_calls,
332           malloc_stats.realloc_calls,
333           malloc_stats.calloc_calls);
334}
335
336
337void malloc_walk(size_t source, size_t printf_enabled)
338{
339   register Region_Control *the_region;
340   Objects_Locations        location;
341 
342   the_region = _Region_Get( RTEMS_Malloc_Heap, &location );
343   if ( location == OBJECTS_LOCAL )
344   {
345      _Heap_Walk( &the_region->Memory, source, printf_enabled );
346      _Thread_Enable_dispatch();
347   }
348}
349
350#else
351
352void malloc_dump(void)
353{
354   return;
355}
356 
357void malloc_walk(size_t source, size_t printf_enabled)
358{
359   return;
360}
361
362#endif
363
364/*
365 *  "Reentrant" versions of the above routines implemented above.
366 */
367
368#ifdef RTEMS_NEWLIB
369void *malloc_r(
370  struct _reent *ignored,
371  size_t  size
372)
373{
374  return malloc( size );
375}
376
377void *calloc_r(
378  size_t nelem,
379  size_t elsize
380)
381{
382  return calloc( nelem, elsize );
383}
384
385void *realloc_r(
386  void *ptr,
387  size_t size
388)
389{
390  return realloc_r( ptr, size );
391}
392
393void free_r(
394  void *ptr
395)
396{
397  free( ptr );
398}
399#endif
400
Note: See TracBrowser for help on using the repository browser.