linux/bootstrap: use Forbid/Permit only when thread is main AROS thread
[AROS.git] / arch / all-linux / bootstrap / malloc.c
blobb2a1f000f5d89d4d4d1e65f6be2410adc9f8ab88
1 /*
2 Copyright © 1995-2014, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 /*
7 * AROS-thread-safe versions of libc memory allocation routines
8 * This does not work with Android's bionic.
9 */
11 /* TODO:
12 This code is compiled with the kernel compiler but calls code
13 from exec.library. This can only work if function calling
14 convention for kernel and target compiler are the same.
15 Up to now this seems to be the case for all linux hosted ports
16 as UNIX calling is often taken as reference for the AROS
17 implementation.
20 #ifndef __ANDROID__
22 #include <proto/exec.h>
23 #include <sys/types.h>
25 #include <unistd.h>
26 #include <syscall.h>
28 static int memnest;
29 extern pid_t arostid;
31 #define THREADID pid_t thistid = syscall(SYS_gettid);
32 #define MEMLOCK if (SysBase != NULL && thistid == arostid) Forbid();
33 #define MEMUNLOCK if (SysBase != NULL && thistid == arostid) Permit();
35 extern struct ExecBase *SysBase;
36 extern void * __libc_malloc(size_t);
37 extern void __libc_free(void *);
38 extern void * __libc_calloc(size_t, size_t);
39 extern void * __libc_realloc(void * mem, size_t newsize);
41 void * malloc(size_t size)
43 void *res;
44 THREADID
46 MEMLOCK
47 memnest++;
48 res = __libc_malloc(size);
49 memnest--;
50 MEMUNLOCK
52 return res;
55 void free(void * addr)
57 THREADID
59 MEMLOCK
60 memnest++;
61 __libc_free(addr);
62 memnest--;
63 MEMUNLOCK
66 void * calloc(size_t n, size_t size)
68 void *res;
69 THREADID
71 MEMLOCK
72 memnest++;
73 res = __libc_calloc(n, size);
74 memnest--;
75 MEMUNLOCK
77 return res;
80 void *realloc(void *ptr, size_t size)
82 void *res;
83 THREADID
85 MEMLOCK
86 memnest++;
87 res = __libc_realloc(ptr, size);
88 memnest--;
89 MEMUNLOCK;
91 return res;
94 #endif