r6831@lvps87-230-33-50: verhaegs | 2008-02-03 14:08:57 +0100
[tangerine.git] / compiler / clib / malloc.c
blobde7e503bf4e07005d56fb51d8fd259697e1b8719
1 /*
2 Copyright © 1995-2007, The AROS Development Team. All rights reserved.
3 $Id$
5 ANSI C function malloc().
6 */
8 #include "__arosc_privdata.h"
10 #include <errno.h>
11 #include <dos/dos.h>
12 #include <exec/memory.h>
13 #include <proto/exec.h>
14 #include <aros/symbolsets.h>
15 #include <aros/debug.h>
17 /*****************************************************************************
19 NAME */
20 #include <sys/types.h>
21 #include <stdlib.h>
23 void *malloc (
25 /* SYNOPSIS */
26 size_t size)
28 /* FUNCTION
29 Allocate size bytes of memory and return the address of the
30 first byte.
32 INPUTS
33 size - How much memory to allocate.
35 RESULT
36 A pointer to the allocated memory or NULL. If you don't need the
37 memory anymore, you can pass this pointer to free(). If you don't,
38 the memory will be freed for you when the application exits.
40 NOTES
41 This function must not be used in a shared library or in a threaded
42 application.
44 EXAMPLE
46 BUGS
48 SEE ALSO
49 free()
51 INTERNALS
53 ******************************************************************************/
55 UBYTE *mem = NULL;
57 /* Allocate the memory */
58 mem = AllocPooled (__startup_mempool, size + AROS_ALIGN(sizeof(size_t)));
59 if (mem)
61 *((size_t *)mem) = size;
62 mem += AROS_ALIGN(sizeof(size_t));
64 else
65 errno = ENOMEM;
67 return mem;
69 } /* malloc */
72 int __init_memstuff(void)
74 __startup_mempool = CreatePool(MEMF_ANY | MEMF_SEM_PROTECTED, 4096L, 2000L);
76 if (!__startup_mempool)
78 return 0;
81 return 1;
85 void __exit_memstuff(void)
87 if (__startup_mempool)
89 DeletePool(__startup_mempool);
93 ADD2INIT(__init_memstuff, 0);
94 ADD2EXIT(__exit_memstuff, 0);