4 #define bcopy(s,d,n) memcpy ((d), (s), (n)) /* No overlap handling. */
6 #include "hurdmalloc.h" /* XXX see that file */
9 #define vm_allocate __vm_allocate
10 #define vm_page_size __vm_page_size
13 * Mach Operating System
14 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
15 * All Rights Reserved.
17 * Permission to use, copy, modify and distribute this software and its
18 * documentation is hereby granted, provided that both the copyright
19 * notice and this permission notice appear in all copies of the
20 * software, derivative works or modified versions, and any portions
21 * thereof, and that both notices appear in supporting documentation.
23 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
24 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
25 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
27 * Carnegie Mellon requests users of this software to return to
29 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
30 * School of Computer Science
31 * Carnegie Mellon University
32 * Pittsburgh PA 15213-3890
34 * any improvements or extensions that they make and grant Carnegie Mellon
35 * the rights to redistribute these changes.
40 * Revision 1.10 1995/02/13 22:04:34 roland
41 * (malloc_init): Add self reference to avoid not only the `defined but not
42 * used' warning, but also to avoid GCC optimizing out the entire function
45 * Revision 1.9 1995/02/13 16:36:08 roland
46 * Include string.h; #define bcopy using memcpy.
48 * Revision 1.8 1995/02/03 01:54:21 roland
49 * Remove bogus bcopy decl.
51 * Revision 1.7 1995/01/26 04:22:02 roland
52 * Don't include gnu-stabs.h.
54 * Revision 1.6 1994/12/07 19:41:26 roland
55 * (vm_allocate, vm_page_size): #define these to __ names at top.
57 * Revision 1.5 1994/06/04 01:48:44 roland
60 * Revision 2.7 91/05/14 17:57:34 mrt
61 * Correcting copyright
63 * Revision 2.6 91/02/14 14:20:26 mrt
64 * Added new Mach copyright
65 * [91/02/13 12:41:21 mrt]
67 * Revision 2.5 90/11/05 14:37:33 rpd
68 * Added malloc_fork* code.
74 * Revision 2.4 90/08/07 14:31:28 rpd
75 * Removed RCS keyword nonsense.
77 * Revision 2.3 90/06/02 15:14:00 rpd
78 * Converted to new IPC.
79 * [90/03/20 20:56:57 rpd]
81 * Revision 2.2 89/12/08 19:53:59 rwd
82 * Removed conditionals.
85 * Revision 2.1 89/08/03 17:09:46 rwd
89 * 13-Sep-88 Eric Cooper (ecc) at Carnegie Mellon University
90 * Changed realloc() to copy min(old size, new size) bytes.
91 * Bug found by Mike Kupfer at Olivetti.
95 * Author: Eric Cooper, Carnegie Mellon University
98 * Memory allocator for use with multiple threads.
102 #include <cthreads.h>
103 #include "cthread_internals.h"
107 * Structure of memory block header.
108 * When free, next points to next block on free list.
109 * When allocated, fl points to free list.
110 * Size of header is 4 bytes, so minimum usable block size is 8 bytes.
112 typedef union header
{
114 struct free_list
*fl
;
117 #define MIN_SIZE 8 /* minimum block size */
119 typedef struct free_list
{
120 spin_lock_t lock
; /* spin lock for mutual exclusion */
121 header_t head
; /* head of free list for this size */
123 int in_use
; /* # mallocs - # frees */
128 * Free list with index i contains blocks of size 2^(i+3) including header.
129 * Smallest block size is 8, with 4 bytes available to user.
130 * Size argument to malloc is a signed integer for sanity checking,
131 * so largest block size is 2^31.
135 static struct free_list malloc_free_list
[NBUCKETS
];
137 /* Initialization just sets everything to zero, but might be necessary on a
138 machine where spin_lock_init does otherwise, and is necessary when
139 running an executable that was written by something like Emacs's unexec.
140 It preserves the values of data variables like malloc_free_list, but
141 does not save the vm_allocate'd space allocated by this malloc. */
147 for (i
= 0; i
< NBUCKETS
; ++i
)
149 spin_lock_init (&malloc_free_list
[i
].lock
);
150 malloc_free_list
[i
].head
= NULL
;
152 malloc_free_list
[i
].in_use
= 0;
156 /* This not only suppresses a `defined but not used' warning,
157 but it is ABSOLUTELY NECESSARY to avoid the hyperclever
158 compiler from "optimizing out" the entire function! */
163 more_memory(size
, fl
)
165 register free_list_t fl
;
173 if (size
<= vm_page_size
) {
174 amount
= vm_page_size
;
175 n
= vm_page_size
/ size
;
177 * We lose vm_page_size - n*size bytes here. */
182 MACH_CALL(vm_allocate(mach_task_self(), &where
, (vm_size_t
) amount
, TRUE
), r
);
183 h
= (header_t
) where
;
187 h
= (header_t
) ((char *) h
+ size
);
191 /* Declaration changed to standard one for GNU. */
194 register size_t size
;
197 register free_list_t fl
;
200 if ((int) size
< 0) /* sanity check */
202 size
+= sizeof(union header
);
204 * Find smallest power-of-two block size
205 * big enough to hold requested size plus header.
213 ASSERT(i
< NBUCKETS
);
214 fl
= &malloc_free_list
[i
];
215 spin_lock(&fl
->lock
);
219 * Free list is empty;
220 * allocate more blocks.
228 spin_unlock(&fl
->lock
);
233 * Pop block from free list.
239 spin_unlock(&fl
->lock
);
241 * Store free list pointer in block header
242 * so we can figure out where it goes
247 * Return pointer past the block header.
249 return ((char *) h
) + sizeof(union header
);
252 /* Declaration changed to standard one for GNU. */
258 register free_list_t fl
;
264 * Find free list for block.
266 h
= (header_t
) (base
- sizeof(union header
));
268 i
= fl
- malloc_free_list
;
272 if (i
< 0 || i
>= NBUCKETS
) {
273 ASSERT(0 <= i
&& i
< NBUCKETS
);
276 if (fl
!= &malloc_free_list
[i
]) {
277 ASSERT(fl
== &malloc_free_list
[i
]);
281 * Push block on free list.
283 spin_lock(&fl
->lock
);
289 spin_unlock(&fl
->lock
);
293 /* Declaration changed to standard one for GNU. */
295 realloc(old_base
, new_size
)
300 register free_list_t fl
;
302 unsigned int old_size
;
306 return malloc (new_size
);
309 * Find size of old block.
311 h
= (header_t
) (old_base
- sizeof(union header
));
313 i
= fl
- malloc_free_list
;
317 if (i
< 0 || i
>= NBUCKETS
) {
318 ASSERT(0 <= i
&& i
< NBUCKETS
);
321 if (fl
!= &malloc_free_list
[i
]) {
322 ASSERT(fl
== &malloc_free_list
[i
]);
326 * Free list with index i contains blocks of size 2^(i+3) including header.
328 old_size
= (1 << (i
+3)) - sizeof(union header
);
330 * Allocate new block, copy old bytes, and free old block.
332 new_base
= malloc(new_size
);
334 bcopy(old_base
, new_base
, (int) (old_size
< new_size
? old_size
: new_size
));
341 print_malloc_free_list()
343 register int i
, size
;
344 register free_list_t fl
;
350 fprintf(stderr
, " Size In Use Free Total\n");
351 for (i
= 0, size
= MIN_SIZE
, fl
= malloc_free_list
;
353 i
+= 1, size
<<= 1, fl
+= 1) {
354 spin_lock(&fl
->lock
);
355 if (fl
->in_use
!= 0 || fl
->head
!= 0) {
356 total_used
+= fl
->in_use
* size
;
357 for (n
= 0, h
= fl
->head
; h
!= 0; h
= h
->next
, n
+= 1)
359 total_free
+= n
* size
;
360 fprintf(stderr
, "%10d %10d %10d %10d\n",
361 size
, fl
->in_use
, n
, fl
->in_use
+ n
);
363 spin_unlock(&fl
->lock
);
365 fprintf(stderr
, " all sizes %10d %10d %10d\n",
366 total_used
, total_free
, total_used
+ total_free
);
370 static void malloc_fork_prepare()
372 * Prepare the malloc module for a fork by insuring that no thread is in a
373 * malloc critical section.
378 for (i
= 0; i
< NBUCKETS
; i
++) {
379 spin_lock(&malloc_free_list
[i
].lock
);
383 static void malloc_fork_parent()
385 * Called in the parent process after a fork() to resume normal operation.
390 for (i
= NBUCKETS
-1; i
>= 0; i
--) {
391 spin_unlock(&malloc_free_list
[i
].lock
);
395 static void malloc_fork_child()
397 * Called in the child process after a fork() to resume normal operation.
402 for (i
= NBUCKETS
-1; i
>= 0; i
--) {
403 spin_unlock(&malloc_free_list
[i
].lock
);
408 text_set_element (_hurd_fork_prepare_hook
, malloc_fork_prepare
);
409 text_set_element (_hurd_fork_parent_hook
, malloc_fork_parent
);
410 text_set_element (_hurd_fork_child_hook
, malloc_fork_child
);
411 text_set_element (_hurd_preinit_hook
, malloc_init
);