4 #include "hurdmalloc.h" /* XXX see that file */
7 #include <mach/spin-lock.h>
8 #define vm_allocate __vm_allocate
9 #define vm_page_size __vm_page_size
12 * Mach Operating System
13 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
14 * All Rights Reserved.
16 * Permission to use, copy, modify and distribute this software and its
17 * documentation is hereby granted, provided that both the copyright
18 * notice and this permission notice appear in all copies of the
19 * software, derivative works or modified versions, and any portions
20 * thereof, and that both notices appear in supporting documentation.
22 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
23 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
24 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
26 * Carnegie Mellon requests users of this software to return to
28 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
29 * School of Computer Science
30 * Carnegie Mellon University
31 * Pittsburgh PA 15213-3890
33 * any improvements or extensions that they make and grant Carnegie Mellon
34 * the rights to redistribute these changes.
39 * Revision 2.7 91/05/14 17:57:34 mrt
40 * Correcting copyright
42 * Revision 2.6 91/02/14 14:20:26 mrt
43 * Added new Mach copyright
44 * [91/02/13 12:41:21 mrt]
46 * Revision 2.5 90/11/05 14:37:33 rpd
47 * Added malloc_fork* code.
53 * Revision 2.4 90/08/07 14:31:28 rpd
54 * Removed RCS keyword nonsense.
56 * Revision 2.3 90/06/02 15:14:00 rpd
57 * Converted to new IPC.
58 * [90/03/20 20:56:57 rpd]
60 * Revision 2.2 89/12/08 19:53:59 rwd
61 * Removed conditionals.
64 * Revision 2.1 89/08/03 17:09:46 rwd
68 * 13-Sep-88 Eric Cooper (ecc) at Carnegie Mellon University
69 * Changed realloc() to copy min(old size, new size) bytes.
70 * Bug found by Mike Kupfer at Olivetti.
74 * Author: Eric Cooper, Carnegie Mellon University
77 * Memory allocator for use with multiple threads.
86 * Structure of memory block header.
87 * When free, next points to next block on free list.
88 * When allocated, fl points to free list.
89 * Size of header is 4 bytes, so minimum usable block size is 8 bytes.
92 #define CHECK_BUSY 0x8a3c743e
93 #define CHECK_FREE 0x66688b92
97 typedef struct header
{
101 struct free_list
*fl
;
105 #define HEADER_SIZE sizeof (struct header)
106 #define HEADER_NEXT(h) ((h)->u.next)
107 #define HEADER_FREE(h) ((h)->u.fl)
108 #define HEADER_CHECK(h) ((h)->check)
110 #define LOG2_MIN_SIZE 4
114 typedef union header
{
116 struct free_list
*fl
;
119 #define HEADER_SIZE sizeof (union header)
120 #define HEADER_NEXT(h) ((h)->next)
121 #define HEADER_FREE(h) ((h)->fl)
122 #define MIN_SIZE 8 /* minimum block size */
123 #define LOG2_MIN_SIZE 3
127 typedef struct free_list
{
128 spin_lock_t lock
; /* spin lock for mutual exclusion */
129 header_t head
; /* head of free list for this size */
131 int in_use
; /* # mallocs - # frees */
136 * Free list with index i contains blocks of size 2 ^ (i + LOG2_MIN_SIZE)
137 * including header. Smallest block size is MIN_SIZE, with MIN_SIZE -
138 * HEADER_SIZE bytes available to user. Size argument to malloc is a signed
139 * integer for sanity checking, so largest block size is 2^31.
143 static struct free_list malloc_free_list
[NBUCKETS
];
145 /* Initialization just sets everything to zero, but might be necessary on a
146 machine where spin_lock_init does otherwise, and is necessary when
147 running an executable that was written by something like Emacs's unexec.
148 It preserves the values of data variables like malloc_free_list, but
149 does not save the vm_allocate'd space allocated by this malloc. */
155 for (i
= 0; i
< NBUCKETS
; ++i
)
157 spin_lock_init (&malloc_free_list
[i
].lock
);
158 malloc_free_list
[i
].head
= NULL
;
160 malloc_free_list
[i
].in_use
= 0;
164 /* This not only suppresses a `defined but not used' warning,
165 but it is ABSOLUTELY NECESSARY to avoid the hyperclever
166 compiler from "optimizing out" the entire function! */
171 more_memory(int size
, free_list_t fl
)
179 if (size
<= vm_page_size
) {
180 amount
= vm_page_size
;
181 n
= vm_page_size
/ size
;
182 /* We lose vm_page_size - n*size bytes here. */
188 r
= vm_allocate(mach_task_self(), &where
, (vm_size_t
) amount
, TRUE
);
191 h
= (header_t
) where
;
193 HEADER_NEXT (h
) = fl
->head
;
195 HEADER_CHECK (h
) = CHECK_FREE
;
198 h
= (header_t
) ((char *) h
+ size
);
202 /* Declaration changed to standard one for GNU. */
210 if ((int) size
< 0) /* sanity check */
214 * Find smallest power-of-two block size
215 * big enough to hold requested size plus header.
223 assert(i
< NBUCKETS
);
224 fl
= &malloc_free_list
[i
];
225 spin_lock(&fl
->lock
);
229 * Free list is empty;
230 * allocate more blocks.
238 spin_unlock(&fl
->lock
);
243 * Pop block from free list.
245 fl
->head
= HEADER_NEXT (h
);
248 assert (HEADER_CHECK (h
) == CHECK_FREE
);
249 HEADER_CHECK (h
) = CHECK_BUSY
;
255 spin_unlock(&fl
->lock
);
257 * Store free list pointer in block header
258 * so we can figure out where it goes
261 HEADER_FREE (h
) = fl
;
263 * Return pointer past the block header.
265 return ((char *) h
) + HEADER_SIZE
;
268 /* Declaration changed to standard one for GNU. */
279 * Find free list for block.
281 h
= (header_t
) (base
- HEADER_SIZE
);
284 assert (HEADER_CHECK (h
) == CHECK_BUSY
);
287 fl
= HEADER_FREE (h
);
288 i
= fl
- malloc_free_list
;
292 if (i
< 0 || i
>= NBUCKETS
) {
293 assert(0 <= i
&& i
< NBUCKETS
);
296 if (fl
!= &malloc_free_list
[i
]) {
297 assert(fl
== &malloc_free_list
[i
]);
301 * Push block on free list.
303 spin_lock(&fl
->lock
);
304 HEADER_NEXT (h
) = fl
->head
;
306 HEADER_CHECK (h
) = CHECK_FREE
;
312 spin_unlock(&fl
->lock
);
316 /* Declaration changed to standard one for GNU. */
318 realloc (void *old_base
, size_t new_size
)
323 unsigned int old_size
;
327 return malloc (new_size
);
330 * Find size of old block.
332 h
= (header_t
) (old_base
- HEADER_SIZE
);
334 assert (HEADER_CHECK (h
) == CHECK_BUSY
);
336 fl
= HEADER_FREE (h
);
337 i
= fl
- malloc_free_list
;
341 if (i
< 0 || i
>= NBUCKETS
) {
342 assert(0 <= i
&& i
< NBUCKETS
);
345 if (fl
!= &malloc_free_list
[i
]) {
346 assert(fl
== &malloc_free_list
[i
]);
350 * Free list with index i contains blocks of size
351 * 2 ^ (i + * LOG2_MIN_SIZE) including header.
353 old_size
= (1 << (i
+ LOG2_MIN_SIZE
)) - HEADER_SIZE
;
355 if (new_size
<= old_size
356 && new_size
> (((old_size
+ HEADER_SIZE
) >> 1) - HEADER_SIZE
))
357 /* The new size still fits in the same block, and wouldn't fit in
358 the next smaller block! */
362 * Allocate new block, copy old bytes, and free old block.
364 new_base
= malloc(new_size
);
366 memcpy (new_base
, old_base
,
367 (int) (old_size
< new_size
? old_size
: new_size
));
369 if (new_base
|| new_size
== 0)
370 /* Free OLD_BASE, but only if the malloc didn't fail. */
378 print_malloc_free_list (void)
387 fprintf(stderr
, " Size In Use Free Total\n");
388 for (i
= 0, size
= MIN_SIZE
, fl
= malloc_free_list
;
390 i
+= 1, size
<<= 1, fl
+= 1) {
391 spin_lock(&fl
->lock
);
392 if (fl
->in_use
!= 0 || fl
->head
!= 0) {
393 total_used
+= fl
->in_use
* size
;
394 for (n
= 0, h
= fl
->head
; h
!= 0; h
= HEADER_NEXT (h
), n
+= 1)
396 total_free
+= n
* size
;
397 fprintf(stderr
, "%10d %10d %10d %10d\n",
398 size
, fl
->in_use
, n
, fl
->in_use
+ n
);
400 spin_unlock(&fl
->lock
);
402 fprintf(stderr
, " all sizes %10d %10d %10d\n",
403 total_used
, total_free
, total_used
+ total_free
);
408 _hurd_malloc_fork_prepare(void)
410 * Prepare the malloc module for a fork by insuring that no thread is in a
411 * malloc critical section.
416 for (i
= 0; i
< NBUCKETS
; i
++) {
417 spin_lock(&malloc_free_list
[i
].lock
);
422 _hurd_malloc_fork_parent(void)
424 * Called in the parent process after a fork() to resume normal operation.
429 for (i
= NBUCKETS
-1; i
>= 0; i
--) {
430 spin_unlock(&malloc_free_list
[i
].lock
);
435 _hurd_malloc_fork_child(void)
437 * Called in the child process after a fork() to resume normal operation.
442 for (i
= NBUCKETS
-1; i
>= 0; i
--) {
443 spin_unlock(&malloc_free_list
[i
].lock
);
448 text_set_element (_hurd_preinit_hook
, malloc_init
);