OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [services/] [memalloc/] [common/] [current/] [src/] [malloc.cxx] - Blame information for rev 819

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 786 skrzyp
//========================================================================
2
//
3
//      malloc.cxx
4
//
5
//      Implementation of ISO C memory allocation routines
6
//
7
//========================================================================
8
// ####ECOSGPLCOPYRIGHTBEGIN####                                            
9
// -------------------------------------------                              
10
// This file is part of eCos, the Embedded Configurable Operating System.   
11
// Copyright (C) 1998, 1999, 2000, 2001, 2002, 2004, 2009 Free Software Foundation, Inc.
12
//
13
// eCos is free software; you can redistribute it and/or modify it under    
14
// the terms of the GNU General Public License as published by the Free     
15
// Software Foundation; either version 2 or (at your option) any later      
16
// version.                                                                 
17
//
18
// eCos is distributed in the hope that it will be useful, but WITHOUT      
19
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or    
20
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License    
21
// for more details.                                                        
22
//
23
// You should have received a copy of the GNU General Public License        
24
// along with eCos; if not, write to the Free Software Foundation, Inc.,    
25
// 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.            
26
//
27
// As a special exception, if other files instantiate templates or use      
28
// macros or inline functions from this file, or you compile this file      
29
// and link it with other works to produce a work based on this file,       
30
// this file does not by itself cause the resulting work to be covered by   
31
// the GNU General Public License. However the source code for this file    
32
// must still be made available in accordance with section (3) of the GNU   
33
// General Public License v2.                                               
34
//
35
// This exception does not invalidate any other reasons why a work based    
36
// on this file might be covered by the GNU General Public License.         
37
// -------------------------------------------                              
38
// ####ECOSGPLCOPYRIGHTEND####                                              
39
//========================================================================
40
//#####DESCRIPTIONBEGIN####
41
//
42
// Author(s):     jlarmour
43
// Contributors:  
44
// Date:          2000-04-30
45
// Purpose:       Provides ISO C calloc(), malloc(), realloc() and free()
46
//                functions
47
// Description:   Implementation of ISO standard allocation routines as per
48
//                ISO C section 7.10.3
49
// Usage:       
50
//
51
//####DESCRIPTIONEND####
52
//
53
//========================================================================
54
 
55
// CONFIGURATION
56
 
57
#include <pkgconf/memalloc.h>   // Configuration header
58
 
59
// Do we want these functions?
60
#ifdef CYGPKG_MEMALLOC_MALLOC_ALLOCATORS
61
 
62
// INCLUDES
63
 
64
#include <cyg/infra/cyg_type.h>    // Common type definitions and support
65
#include <cyg/infra/cyg_trac.h>    // Common tracing support
66
#include <cyg/infra/cyg_ass.h>     // Common assertion support
67
#include <string.h>                // For memset() and memmove()
68
#include <stdlib.h>                // header for this file
69
#ifdef CYGBLD_MEMALLOC_MALLOC_EXTERNAL_HEAP_H
70
# include CYGBLD_MEMALLOC_MALLOC_EXTERNAL_HEAP_H
71
#else
72
# include <pkgconf/heaps.hxx>       // heap pools information
73
#endif
74
#include CYGBLD_MEMALLOC_MALLOC_IMPLEMENTATION_HEADER
75
 
76
// STATIC VARIABLES
77
 
78
// First deal with the worst case, that the memory layout didn't define a
79
// heap
80
#if CYGMEM_HEAP_COUNT == 0
81
 
82
// the data space for the memory pool
83
cyg_uint8 cyg_memalloc_mallocpool_memory[
84
    CYGNUM_MEMALLOC_FALLBACK_MALLOC_POOL_SIZE ] CYGBLD_ATTRIB_WEAK;
85
 
86
// the memory pool object itself
87
CYGCLS_MEMALLOC_MALLOC_IMPL cyg_memalloc_mallocpool
88
   CYGBLD_ATTRIB_INIT_PRI( CYG_INIT_MEMALLOC ) =
89
   CYGCLS_MEMALLOC_MALLOC_IMPL( cyg_memalloc_mallocpool_memory,
90
                                sizeof( cyg_memalloc_mallocpool_memory ) );
91
 
92
# define POOL cyg_memalloc_mallocpool
93
 
94
#elif CYGMEM_HEAP_COUNT == 1
95
// one heap, so it's straightforward
96
 
97
# define POOL (*cygmem_memalloc_heaps[0])
98
 
99
#else 
100
// multiple heaps
101
#ifdef CYGBLD_MEMALLOC_MALLOC_EXTERNAL_JOIN_H
102
# include CYGBLD_MEMALLOC_MALLOC_EXTERNAL_JOIN_H
103
#else
104
# include <cyg/memalloc/memjoin.hxx>
105
#endif
106
 
107
Cyg_Mempool_Joined<CYGCLS_MEMALLOC_MALLOC_IMPL> cyg_memalloc_mallocpool
108
   CYGBLD_ATTRIB_INIT_PRI( CYG_INIT_MEMALLOC ) =
109
     Cyg_Mempool_Joined<CYGCLS_MEMALLOC_MALLOC_IMPL>(
110
       CYGMEM_HEAP_COUNT, cygmem_memalloc_heaps
111
     );
112
 
113
# define POOL cyg_memalloc_mallocpool
114
 
115
#endif
116
 
117
// FUNCTIONS
118
 
119
void *
120
malloc( size_t size )
121
{
122
    void *data_ptr;
123
 
124
    CYG_REPORT_FUNCNAMETYPE( "malloc", "returning pointer %08x" );
125
 
126
    CYG_REPORT_FUNCARG1DV( size );
127
 
128
#ifdef CYGSEM_MEMALLOC_MALLOC_ZERO_RETURNS_NULL
129
    // first check if size wanted is 0
130
    if ( 0 == size ) {
131
        CYG_REPORT_RETVAL( NULL );
132
        return NULL;
133
    } // if
134
#endif
135
 
136
    // ask the pool for the data
137
    data_ptr = POOL.try_alloc( size );
138
 
139
    // if it isn't NULL is the pointer valid?
140
    if ( NULL != data_ptr ) {
141
        CYG_CHECK_DATA_PTR( data_ptr,
142
                            "allocator returned invalid pointer!" );
143
 
144
        // And just check its alignment
145
        CYG_ASSERT( !((CYG_ADDRWORD)data_ptr & (sizeof(CYG_ADDRWORD) - 1)),
146
                    "Allocator has returned badly aligned data!");
147
    } // if
148
 
149
    CYG_REPORT_RETVAL( data_ptr );
150
 
151
    return data_ptr;
152
} // malloc()
153
 
154
 
155
void
156
free( void *ptr )
157
{
158
    cyg_bool freeret CYGBLD_ATTRIB_UNUSED;
159
 
160
    CYG_REPORT_FUNCNAME( "free");
161
 
162
    CYG_REPORT_FUNCARG1XV( ptr );
163
 
164
    // if null pointer, do nothing as per spec
165
    if ( NULL==ptr )
166
        return;
167
 
168
    CYG_CHECK_DATA_PTR( ptr, "Pointer to free isn't even valid!" );
169
 
170
    // get pool to free it
171
    freeret = POOL.free( (cyg_uint8 *) ptr );
172
 
173
    CYG_ASSERT( freeret , "Couldn't free!" );
174
 
175
    CYG_REPORT_RETURN();
176
 
177
} // free()
178
 
179
 
180
void *
181
calloc( size_t nmemb, size_t size )
182
{
183
    void *data_ptr;
184
    cyg_ucount32 realsize;
185
 
186
    CYG_REPORT_FUNCNAMETYPE( "calloc", "returning pointer %08x" );
187
 
188
    CYG_REPORT_FUNCARG2DV( nmemb, size );
189
 
190
    realsize = nmemb * size;
191
 
192
    data_ptr = malloc( realsize );
193
 
194
    // Fill with 0's if non-NULL
195
    if ( data_ptr != NULL )
196
        memset( data_ptr, 0, realsize );
197
 
198
    CYG_REPORT_RETVAL( data_ptr );
199
    return data_ptr;
200
} // calloc()
201
 
202
 
203
externC void *
204
realloc( void *ptr, size_t size )
205
{
206
    cyg_int32 oldsize;
207
 
208
    CYG_REPORT_FUNCNAMETYPE( "realloc", "returning pointer %08x" );
209
 
210
    CYG_REPORT_FUNCARG2( "ptr=%08x, size=%d", ptr, size );
211
 
212
    // if pointer is NULL, we must malloc it
213
    if ( ptr == NULL ) {
214
        ptr = malloc( size );
215
        CYG_REPORT_RETVAL( ptr );
216
        return ptr;
217
    } // if
218
 
219
    CYG_CHECK_DATA_PTR( ptr, "realloc() passed a bogus pointer!" );
220
 
221
    // if size is 0, we must free it
222
    if (size == 0) {
223
        free(ptr);
224
        CYG_REPORT_RETVAL( NULL );
225
        return NULL;
226
    } // if
227
 
228
    void *newptr;
229
 
230
    // otherwise try to resize allocation
231
    newptr = POOL.resize_alloc( (cyg_uint8 *)ptr, size, &oldsize );
232
 
233
    if ( NULL == newptr ) {
234
        // if resize_alloc doesn't return a pointer, it failed, so we
235
        // just have to allocate new space instead, and later copy it
236
 
237
        CYG_ASSERT( oldsize != 0,
238
                    "resize_alloc() couldn't determine allocation size!" );
239
 
240
        newptr = malloc( size );
241
 
242
        if ( NULL != newptr ) {
243
            memcpy( newptr, ptr, size < (size_t) oldsize ? size
244
                    : (size_t) oldsize );
245
            free( ptr );
246
        }
247
    }
248
 
249
    CYG_REPORT_RETVAL( newptr );
250
    return newptr;
251
} // realloc()
252
 
253
 
254
externC struct mallinfo
255
mallinfo( void )
256
{
257
    struct mallinfo ret = { 0 }; // initialize to all zeros
258
    Cyg_Mempool_Status stat;
259
 
260
    CYG_REPORT_FUNCTION();
261
 
262
    POOL.get_status( CYG_MEMPOOL_STAT_ARENASIZE|
263
                     CYG_MEMPOOL_STAT_FREEBLOCKS|
264
                     CYG_MEMPOOL_STAT_TOTALALLOCATED|
265
                     CYG_MEMPOOL_STAT_TOTALFREE|
266
                     CYG_MEMPOOL_STAT_MAXFREE, stat );
267
 
268
    if ( stat.arenasize > 0 )
269
        ret.arena = stat.arenasize;
270
 
271
    if ( stat.freeblocks > 0 )
272
        ret.ordblks = stat.freeblocks;
273
 
274
    if ( stat.totalallocated > 0 )
275
        ret.uordblks = stat.totalallocated;
276
 
277
    if ( stat.totalfree > 0 )
278
        ret.fordblks = stat.totalfree;
279
 
280
    if ( stat.maxfree > 0 )
281
        ret.maxfree = stat.maxfree;
282
 
283
    CYG_REPORT_RETURN();
284
    return ret;
285
} // mallinfo()
286
 
287
 
288
inline void *
289
operator new(size_t size,  CYGCLS_MEMALLOC_MALLOC_IMPL *ptr)
290
{ return (void *)ptr; };
291
 
292
externC cyg_bool
293
cyg_memalloc_heap_reinit( cyg_uint8 *base, cyg_uint32 size )
294
{
295
#if CYGMEM_HEAP_COUNT == 0
296
    CYGCLS_MEMALLOC_MALLOC_IMPL *m = new(&cyg_memalloc_mallocpool)
297
        CYGCLS_MEMALLOC_MALLOC_IMPL( base, size );
298
    return true;
299
#elif CYGMEM_HEAP_COUNT == 1    
300
    cygmem_memalloc_heaps[0] = new(cygmem_memalloc_heaps[0])
301
        CYGCLS_MEMALLOC_MALLOC_IMPL( base, size );
302
    return true;
303
#else
304
    return false;
305
#endif
306
}
307
 
308
#endif // ifdef CYGPKG_MEMALLOC_MALLOC_ALLOCATORS
309
 
310
// EOF malloc.cxx

powered by: WebSVN 2.1.0

© copyright 1999-2025 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.