]> git.ipfire.org Git - thirdparty/gcc.git/blame - libobjc/memory.c
Update copyright years.
[thirdparty/gcc.git] / libobjc / memory.c
CommitLineData
c3a945cd 1/* GNU Objective C Runtime Memory allocation functions
f1717362 2 Copyright (C) 1993-2016 Free Software Foundation, Inc.
8a7d0ecc 3 Contributed by Kresten Krab Thorup
4
a622d84f 5This file is part of GCC.
8a7d0ecc 6
a622d84f 7GCC is free software; you can redistribute it and/or modify it
8a7d0ecc 8under the terms of the GNU General Public License as published by the
6bc9506f 9Free Software Foundation; either version 3, or (at your option) any
8a7d0ecc 10later version.
11
a622d84f 12GCC is distributed in the hope that it will be useful, but WITHOUT
8a7d0ecc 13ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15for more details.
16
6bc9506f 17Under Section 7 of GPL version 3, you are granted additional
18permissions described in the GCC Runtime Library Exception, version
193.1, as published by the Free Software Foundation.
20
21You should have received a copy of the GNU General Public License and
22a copy of the GCC Runtime Library Exception along with this program;
23see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24<http://www.gnu.org/licenses/>. */
8a7d0ecc 25
2f8eaca5 26/* This file includes the standard functions for memory allocation and
27 disposal. Users should use these functions in their ObjC programs
28 so that they work properly with garbage collectors. */
bb882a43 29
3c744362 30/* TODO: Turn these into macros or inline functions. */
31
e58aa1bc 32#include "objc-private/common.h"
c3a945cd 33#include "objc-private/error.h"
8a7d0ecc 34
e58aa1bc 35/* __USE_FIXED_PROTOTYPES__ used to be required to get prototypes for
36 malloc, free, etc. on some platforms. It is unclear if we still
2f8eaca5 37 need it, but it can't hurt. */
8a7d0ecc 38#define __USE_FIXED_PROTOTYPES__
39#include <stdlib.h>
e58aa1bc 40
6ee7a276 41#include "objc/runtime.h"
8a7d0ecc 42
bb882a43 43#if OBJC_WITH_GC
44#include <gc.h>
8a7d0ecc 45
46void *
61776355 47objc_malloc (size_t size)
8a7d0ecc 48{
bb882a43 49 void *res = (void *)(GC_malloc (size));
61776355 50 if (! res)
c3a945cd 51 _objc_abort ("Virtual memory exhausted\n");
8a7d0ecc 52 return res;
53}
54
55void *
61776355 56objc_atomic_malloc (size_t size)
8a7d0ecc 57{
bb882a43 58 void *res = (void *)(GC_malloc_atomic (size));
61776355 59 if (! res)
c3a945cd 60 _objc_abort ("Virtual memory exhausted\n");
8a7d0ecc 61 return res;
62}
63
64void *
bb882a43 65objc_realloc (void *mem, size_t size)
66{
67 void *res = (void *)(GC_realloc (mem, size));
68 if (! res)
69 _objc_abort ("Virtual memory exhausted\n");
70 return res;
71}
72
73void *
74objc_calloc (size_t nelem, size_t size)
75{
76 /* Note that GC_malloc returns cleared memory (see documentation) so
77 there is no need to clear it. */
db7e3326 78 void *res = (void *)(GC_malloc (nelem * size));
bb882a43 79 if (! res)
80 _objc_abort ("Virtual memory exhausted\n");
81 return res;
82}
83
84void
3c744362 85objc_free (void *mem __attribute__ ((__unused__)))
bb882a43 86{
87 return;
88}
89
90#else
91
92void *
93objc_malloc (size_t size)
8a7d0ecc 94{
bb882a43 95 void *res = (void *)(malloc (size));
96 if (! res)
97 _objc_abort ("Virtual memory exhausted\n");
98 return res;
99}
100
101void *
102objc_atomic_malloc (size_t size)
103{
104 void *res = (void *)(malloc (size));
61776355 105 if (! res)
c3a945cd 106 _objc_abort ("Virtual memory exhausted\n");
8a7d0ecc 107 return res;
108}
109
110void *
61776355 111objc_realloc (void *mem, size_t size)
8a7d0ecc 112{
bb882a43 113 void *res = (void *)(realloc (mem, size));
61776355 114 if (! res)
c3a945cd 115 _objc_abort ("Virtual memory exhausted\n");
8a7d0ecc 116 return res;
117}
118
119void *
61776355 120objc_calloc (size_t nelem, size_t size)
8a7d0ecc 121{
bb882a43 122 void *res = (void *)(calloc (nelem, size));
61776355 123 if (! res)
c3a945cd 124 _objc_abort ("Virtual memory exhausted\n");
8a7d0ecc 125 return res;
126}
127
128void
61776355 129objc_free (void *mem)
8a7d0ecc 130{
bb882a43 131 free (mem);
8a7d0ecc 132}
133
bb882a43 134#endif /* !OBJC_WITH_GC */