+ Update 052_Rob_Ennals.pdf, courtesy of Elizabeth Mattijsen.
[parrot.git] / src / memory.c
blob278266b6bcb34f8c21f80f17c29bf9839e99b5b0
1 /*
2 Copyright (C) 2001-2006, The Perl Foundation.
3 $Id$
5 =head1 NAME
7 src/memory.c - Memory allocation
9 =head1 DESCRIPTION
11 The memory (mem) API handles memory allocation,
13 Basically just a wrapper C<around malloc/calloc/realloc/free()> with an
14 setup function to initialize the memory pools.
16 =head2 Functions
18 =over 4
20 =cut
24 #include "parrot/parrot.h"
26 /* for PANIC */
27 #define interp NULL
31 =item C<void *
32 mem_sys_allocate(size_t size)>
34 Uses C<malloc> to allocate system memory.
36 =cut
40 void *
41 mem_sys_allocate(size_t size)
43 void * const ptr = malloc((size_t)size);
44 #ifdef DETAIL_MEMORY_DEBUG
45 fprintf(stderr, "Allocated %i at %p\n", size, ptr);
46 #endif
47 if (!ptr)
48 PANIC("Out of mem");
49 return ptr;
52 void *
53 mem__internal_allocate(size_t size, const char *file, int line)
55 void * const ptr = malloc((size_t)size);
56 #ifdef DETAIL_MEMORY_DEBUG
57 fprintf(stderr, "Internal malloc %i at %p (%s/%d)\n",
58 size, ptr, file, line);
59 #endif
60 if (!ptr)
61 PANIC("Out of mem");
62 return ptr;
67 =item C<void *
68 mem_sys_allocate_zeroed(size_t size)>
70 Uses C<calloc> to allocate system memory.
72 =cut
76 void *
77 mem_sys_allocate_zeroed(size_t size)
79 void * const ptr = calloc(1, (size_t)size);
80 #ifdef DETAIL_MEMORY_DEBUG
81 fprintf(stderr, "Allocated %i at %p\n", size, ptr);
82 #endif
83 if (!ptr && size)
84 PANIC("Out of mem");
85 return ptr;
88 void *
89 mem__internal_allocate_zeroed(size_t size, const char *file, int line)
91 void * const ptr = calloc(1, (size_t)size);
92 #ifdef DETAIL_MEMORY_DEBUG
93 fprintf(stderr, "Internal malloc %i at %p (%s/%d)\n",
94 size, ptr, file, line);
95 #endif
96 if (!ptr && size)
97 PANIC("Out of mem");
98 return ptr;
103 =item C<void *
104 mem_sys_realloc(void *from, size_t size)>
106 Resize a chunk of system memory.
108 =cut
112 void *
113 mem__sys_realloc(void *from, size_t size)
115 void *ptr;
116 #ifdef DETAIL_MEMORY_DEBUG
117 fprintf(stderr, "Freed %p (realloc -- %i bytes)\n", from, size);
118 #endif
119 ptr = realloc(from, size);
120 #ifdef DETAIL_MEMORY_DEBUG
121 fprintf(stderr, "Allocated %i at %p\n", size, ptr);
122 #endif
123 if (!ptr)
124 PANIC("Out of mem");
125 return ptr;
128 void *
129 mem__internal_realloc(void *from, size_t size, const char *file, int line)
131 void * const ptr = realloc(from, size);
132 #ifdef DETAIL_MEMORY_DEBUG
133 fprintf(stderr, "internal free of %p (realloc -- %i bytes) (%s/%d)\n",
134 from, size, file, line);
135 fprintf(stderr, "Internal malloc %i at %p (%s/%d)\n",
136 size, ptr, file, line);
137 #endif
138 if (!ptr)
139 PANIC("Out of mem");
140 return ptr;
142 #undef interp
146 =item C<void
147 mem_sys_free(void *from)>
149 Free a chunk of memory back to the system.
151 =cut
155 void
156 mem_sys_free(void *from)
158 #ifdef DETAIL_MEMORY_DEBUG
159 fprintf(stderr, "Freed %p\n", from);
160 #endif
161 if (from)
162 free(from);
165 void
166 mem__internal_free(void *from, const char *file, int line)
168 #ifdef DETAIL_MEMORY_DEBUG
169 fprintf(stderr, "Internal free of %p (%s/%d)\n", from, file, line);
170 #endif
171 free(from);
176 =item C<void
177 mem_setup_allocator(Interp *interp)>
179 Initializes the allocator.
181 =cut
185 void
186 mem_setup_allocator(Interp *interp)
188 interp->arena_base = mem_sys_allocate_zeroed(sizeof (struct Arenas));
189 SET_NULL(interp->arena_base->sized_header_pools);
191 #if PARROT_GC_MS
192 Parrot_gc_ms_init(interp);
193 #endif
194 #if PARROT_GC_IMS
195 Parrot_gc_ims_init(interp);
196 #endif
197 #if PARROT_GC_GMS
198 Parrot_gc_gms_init(interp);
199 #endif
201 Parrot_initialize_memory_pools(interp);
202 Parrot_initialize_header_pools(interp);
208 =back
210 =cut
216 * Local variables:
217 * c-file-style: "parrot"
218 * End:
219 * vim: expandtab shiftwidth=4: