Upgraded GRUB2 to 2.00 release.
[AROS.git] / compiler / clib / malloc.c
blobbddaa5fca4cbb21f018fdb3c6d741120b95a1c01
1 /*
2 Copyright © 1995-2012, The AROS Development Team. All rights reserved.
3 $Id$
5 C99 function malloc().
6 */
8 #include "__arosc_privdata.h"
10 #define DEBUG 0
12 #include <errno.h>
13 #include <dos/dos.h>
14 #include <exec/memory.h>
15 #include <proto/exec.h>
16 #include <aros/symbolsets.h>
17 #include <aros/debug.h>
19 /*****************************************************************************
21 NAME */
22 #include <stdlib.h>
24 void *malloc (
26 /* SYNOPSIS */
27 size_t size)
29 /* FUNCTION
30 Allocate size bytes of memory and return the address of the
31 first byte.
33 INPUTS
34 size - How much memory to allocate.
36 RESULT
37 A pointer to the allocated memory or NULL. If you don't need the
38 memory anymore, you can pass this pointer to free(). If you don't,
39 the memory will be freed for you when the application exits.
41 NOTES
42 This function must not be used in a shared library or in a threaded
43 application.
45 EXAMPLE
47 BUGS
49 SEE ALSO
50 free()
52 INTERNALS
54 ******************************************************************************/
56 struct aroscbase *aroscbase = __aros_getbase();
57 UBYTE *mem = NULL;
59 /* Allocate the memory */
60 mem = AllocPooled (aroscbase->acb_mempool, size + AROS_ALIGN(sizeof(size_t)));
61 if (mem)
63 *((size_t *)mem) = size;
64 mem += AROS_ALIGN(sizeof(size_t));
66 else
67 errno = ENOMEM;
69 return mem;
71 } /* malloc */
74 int __init_memstuff(struct aroscbase *aroscbase)
76 D(bug("__init_memstuff: task(%x), aroscbase(%x)\n",
77 FindTask(NULL), aroscbase
78 ));
80 aroscbase->acb_mempool = CreatePool(MEMF_ANY | MEMF_SEM_PROTECTED, 65536L, 4096L);
82 D(bug("__init_memstuff: aroscbase->acb_mempool(%x)\n", aroscbase->acb_mempool));
84 if (!aroscbase->acb_mempool)
86 return 0;
89 return 1;
93 void __exit_memstuff(struct aroscbase *aroscbase)
95 D(bug("__exit_memstuff: task(%x), aroscbase(%x), acb_mempool(%x)\n",
96 FindTask(NULL), aroscbase, aroscbase->acb_mempool
97 ));
99 if (aroscbase->acb_mempool)
101 DeletePool(aroscbase->acb_mempool);
105 ADD2OPENLIB(__init_memstuff, 0);
106 ADD2CLOSELIB(__exit_memstuff, 0);