xref: /netbsd-src/external/gpl3/gcc.old/dist/libobjc/memory.c (revision 8feb0f0b7eaff0608f8350bbfa3098827b4bb91b)
136ac495dSmrg /* GNU Objective C Runtime Memory allocation functions
2*8feb0f0bSmrg    Copyright (C) 1993-2020 Free Software Foundation, Inc.
336ac495dSmrg    Contributed by Kresten Krab Thorup
436ac495dSmrg 
536ac495dSmrg This file is part of GCC.
636ac495dSmrg 
736ac495dSmrg GCC is free software; you can redistribute it and/or modify it
836ac495dSmrg under the terms of the GNU General Public License as published by the
936ac495dSmrg Free Software Foundation; either version 3, or (at your option) any
1036ac495dSmrg later version.
1136ac495dSmrg 
1236ac495dSmrg GCC is distributed in the hope that it will be useful, but WITHOUT
1336ac495dSmrg ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1436ac495dSmrg FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1536ac495dSmrg for more details.
1636ac495dSmrg 
1736ac495dSmrg Under Section 7 of GPL version 3, you are granted additional
1836ac495dSmrg permissions described in the GCC Runtime Library Exception, version
1936ac495dSmrg 3.1, as published by the Free Software Foundation.
2036ac495dSmrg 
2136ac495dSmrg You should have received a copy of the GNU General Public License and
2236ac495dSmrg a copy of the GCC Runtime Library Exception along with this program;
2336ac495dSmrg see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
2436ac495dSmrg <http://www.gnu.org/licenses/>.  */
2536ac495dSmrg 
2636ac495dSmrg /* This file includes the standard functions for memory allocation and
2736ac495dSmrg    disposal.  Users should use these functions in their ObjC programs
2836ac495dSmrg    so that they work properly with garbage collectors.  */
2936ac495dSmrg 
3036ac495dSmrg /* TODO: Turn these into macros or inline functions.  */
3136ac495dSmrg 
3236ac495dSmrg #include "objc-private/common.h"
3336ac495dSmrg #include "objc-private/error.h"
3436ac495dSmrg 
3536ac495dSmrg /* __USE_FIXED_PROTOTYPES__ used to be required to get prototypes for
3636ac495dSmrg    malloc, free, etc. on some platforms.  It is unclear if we still
3736ac495dSmrg    need it, but it can't hurt.  */
3836ac495dSmrg #define __USE_FIXED_PROTOTYPES__
3936ac495dSmrg #include <stdlib.h>
4036ac495dSmrg 
4136ac495dSmrg #include "objc/runtime.h"
4236ac495dSmrg 
4336ac495dSmrg #if OBJC_WITH_GC
4436ac495dSmrg #include <gc/gc.h>
4536ac495dSmrg 
4636ac495dSmrg void *
objc_malloc(size_t size)4736ac495dSmrg objc_malloc (size_t size)
4836ac495dSmrg {
4936ac495dSmrg   void *res = (void *)(GC_malloc (size));
5036ac495dSmrg   if (! res)
5136ac495dSmrg     _objc_abort ("Virtual memory exhausted\n");
5236ac495dSmrg   return res;
5336ac495dSmrg }
5436ac495dSmrg 
5536ac495dSmrg void *
objc_atomic_malloc(size_t size)5636ac495dSmrg objc_atomic_malloc (size_t size)
5736ac495dSmrg {
5836ac495dSmrg   void *res = (void *)(GC_malloc_atomic (size));
5936ac495dSmrg   if (! res)
6036ac495dSmrg     _objc_abort ("Virtual memory exhausted\n");
6136ac495dSmrg   return res;
6236ac495dSmrg }
6336ac495dSmrg 
6436ac495dSmrg void *
objc_realloc(void * mem,size_t size)6536ac495dSmrg objc_realloc (void *mem, size_t size)
6636ac495dSmrg {
6736ac495dSmrg   void *res = (void *)(GC_realloc (mem, size));
6836ac495dSmrg   if (! res)
6936ac495dSmrg     _objc_abort ("Virtual memory exhausted\n");
7036ac495dSmrg   return res;
7136ac495dSmrg }
7236ac495dSmrg 
7336ac495dSmrg void *
objc_calloc(size_t nelem,size_t size)7436ac495dSmrg objc_calloc (size_t nelem, size_t size)
7536ac495dSmrg {
7636ac495dSmrg   /* Note that GC_malloc returns cleared memory (see documentation) so
7736ac495dSmrg      there is no need to clear it.  */
7836ac495dSmrg   void *res = (void *)(GC_malloc (nelem * size));
7936ac495dSmrg   if (! res)
8036ac495dSmrg     _objc_abort ("Virtual memory exhausted\n");
8136ac495dSmrg   return res;
8236ac495dSmrg }
8336ac495dSmrg 
8436ac495dSmrg void
objc_free(void * mem)8536ac495dSmrg objc_free (void *mem __attribute__ ((__unused__)))
8636ac495dSmrg {
8736ac495dSmrg   return;
8836ac495dSmrg }
8936ac495dSmrg 
9036ac495dSmrg #else
9136ac495dSmrg 
9236ac495dSmrg void *
objc_malloc(size_t size)9336ac495dSmrg objc_malloc (size_t size)
9436ac495dSmrg {
9536ac495dSmrg   void *res = (void *)(malloc (size));
9636ac495dSmrg   if (! res)
9736ac495dSmrg     _objc_abort ("Virtual memory exhausted\n");
9836ac495dSmrg   return res;
9936ac495dSmrg }
10036ac495dSmrg 
10136ac495dSmrg void *
objc_atomic_malloc(size_t size)10236ac495dSmrg objc_atomic_malloc (size_t size)
10336ac495dSmrg {
10436ac495dSmrg   void *res = (void *)(malloc (size));
10536ac495dSmrg   if (! res)
10636ac495dSmrg     _objc_abort ("Virtual memory exhausted\n");
10736ac495dSmrg   return res;
10836ac495dSmrg }
10936ac495dSmrg 
11036ac495dSmrg void *
objc_realloc(void * mem,size_t size)11136ac495dSmrg objc_realloc (void *mem, size_t size)
11236ac495dSmrg {
11336ac495dSmrg   void *res = (void *)(realloc (mem, size));
11436ac495dSmrg   if (! res)
11536ac495dSmrg     _objc_abort ("Virtual memory exhausted\n");
11636ac495dSmrg   return res;
11736ac495dSmrg }
11836ac495dSmrg 
11936ac495dSmrg void *
objc_calloc(size_t nelem,size_t size)12036ac495dSmrg objc_calloc (size_t nelem, size_t size)
12136ac495dSmrg {
12236ac495dSmrg   void *res = (void *)(calloc (nelem, size));
12336ac495dSmrg   if (! res)
12436ac495dSmrg     _objc_abort ("Virtual memory exhausted\n");
12536ac495dSmrg   return res;
12636ac495dSmrg }
12736ac495dSmrg 
12836ac495dSmrg void
objc_free(void * mem)12936ac495dSmrg objc_free (void *mem)
13036ac495dSmrg {
13136ac495dSmrg   free (mem);
13236ac495dSmrg }
13336ac495dSmrg 
13436ac495dSmrg #endif	/* !OBJC_WITH_GC */
135