20
20
You should have received a copy of the GNU Library General Public
21
21
License along with this library; if not, write to the Free
22
22
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA.
25
25
#include "config.h"
27
#include <base/preface.h>
27
#include "GNUstepBase/preface.h"
28
28
#include "mframe.h"
32
({ typeof(V) __v=(V); typeof(A) __a=(A); \
33
__a*((__v+__a-1)/__a); })
37
return the size of an object specified by type
41
objc_sizeof_type(const char* type)
61
return sizeof(unsigned char);
69
return sizeof(unsigned short);
77
return sizeof(unsigned int);
85
return sizeof(unsigned long);
93
return sizeof(double);
104
int len = atoi(type+1);
105
while (isdigit(*++type));
106
return len*objc_aligned_size (type);
114
while (*type != _C_STRUCT_E && *type++ != '='); /* skip "<name>=" */
115
while (*type != _C_STRUCT_E)
117
align = objc_alignof_type (type); /* padd to alignment */
118
acc_size = ROUND (acc_size, align);
119
acc_size += objc_sizeof_type (type); /* add component size */
120
type = objc_skip_typespec (type); /* skip component */
128
while (*type != _C_UNION_E && *type++ != '=') /* do nothing */;
129
while (*type != _C_UNION_E)
131
max_size = MAX (max_size, objc_sizeof_type (type));
132
type = objc_skip_typespec (type);
144
Return the alignment of an object specified by type
148
objc_alignof_type(const char* type)
152
return __alignof__(id);
156
return __alignof__(Class);
160
return __alignof__(SEL);
164
return __alignof__(char);
168
return __alignof__(unsigned char);
172
return __alignof__(short);
176
return __alignof__(unsigned short);
180
return __alignof__(int);
184
return __alignof__(unsigned int);
188
return __alignof__(long);
192
return __alignof__(unsigned long);
196
return __alignof__(float);
200
return __alignof__(double);
205
return __alignof__(char*);
209
while (isdigit(*++type)) /* do nothing */;
210
return objc_alignof_type (type);
214
struct { int x; double y; } fooalign;
215
while(*type != _C_STRUCT_E && *type++ != '=') /* do nothing */;
216
if (*type != _C_STRUCT_E)
217
return MAX (objc_alignof_type (type), __alignof__ (fooalign));
219
return __alignof__ (fooalign);
225
while (*type != _C_UNION_E && *type++ != '=') /* do nothing */;
226
while (*type != _C_UNION_E)
228
maxalign = MAX (maxalign, objc_alignof_type (type));
229
type = objc_skip_typespec (type);
240
The aligned size if the size rounded up to the nearest alignment.
244
objc_aligned_size (const char* type)
246
int size = objc_sizeof_type (type);
247
int align = objc_alignof_type (type);
248
return ROUND (size, align);
252
The size rounded up to the nearest integral of the wordsize, taken
253
to be the size of a void*.
257
objc_promoted_size (const char* type)
259
int size = objc_sizeof_type (type);
260
int wordsize = sizeof (void*);
262
return ROUND (size, wordsize);
266
Skip type qualifiers. These may eventually precede typespecs
267
occuring in method prototype encodings.
271
objc_skip_type_qualifiers (const char* type)
273
while (*type == _C_CONST
277
|| *type == _C_BYCOPY
281
#ifdef _C_GCINVISIBLE
282
|| *type == _C_GCINVISIBLE
284
|| *type == _C_ONEWAY)
293
Skip one typespec element. If the typespec is prepended by type
294
qualifiers, these are skipped as well.
298
objc_skip_typespec (const char* type)
300
type = objc_skip_type_qualifiers (type);
305
/* An id may be annotated by the actual type if it is known
306
with the @"ClassName" syntax */
312
while (*++type != '"') /* do nothing */;
316
/* The following are one character type codes */
336
/* skip digits, typespec and closing ']' */
338
while(isdigit(*++type));
339
type = objc_skip_typespec(type);
340
if (*type == _C_ARY_E)
346
/* skip name, and elements until closing '}' */
348
while (*type != _C_STRUCT_E && *type++ != '=');
349
while (*type != _C_STRUCT_E) { type = objc_skip_typespec (type); }
353
/* skip name, and elements until closing ')' */
355
while (*type != _C_UNION_E && *type++ != '=');
356
while (*type != _C_UNION_E) { type = objc_skip_typespec (type); }
360
/* Just skip the following typespec */
362
return objc_skip_typespec (++type);
370
Skip an offset as part of a method encoding. This is prepended by a
371
'+' if the argument is passed in registers.
374
objc_skip_offset (const char* type)
376
if (*type == '+') type++;
377
if (*type == '-') type++;
378
while(isdigit(*++type));
383
Skip an argument specification of a method encoding.
386
objc_skip_argspec (const char* type)
388
type = objc_skip_typespec (type);
389
type = objc_skip_offset (type);
394
objc_get_type_qualifiers (const char* type)
402
case _C_CONST: res |= _F_CONST; break;
403
case _C_IN: res |= _F_IN; break;
404
case _C_INOUT: res |= _F_INOUT; break;
405
case _C_OUT: res |= _F_OUT; break;
406
case _C_BYCOPY: res |= _F_BYCOPY; break;
408
case _C_BYREF: res |= _F_BYREF; break;
410
case _C_ONEWAY: res |= _F_ONEWAY; break;
411
#ifdef _C_GCINVISIBLE
412
case _C_GCINVISIBLE: res |= _F_GCINVISIBLE; break;
420
/* Returns YES iff t1 and t2 have same method types, but we ignore
421
the argframe layout */
423
sel_types_match (const char* t1, const char* t2)
429
if (*t1 == '+') t1++;
430
if (*t1 == '-') t1++;
431
if (*t2 == '+') t2++;
432
if (*t2 == '-') t2++;
433
while (isdigit(*t1)) t1++;
434
while (isdigit(*t2)) t2++;
435
/* xxx Remove these next two lines when qualifiers are put in
436
all selectors, not just Protocol selectors. */
437
t1 = objc_skip_type_qualifiers(t1);
438
t2 = objc_skip_type_qualifiers(t2);
449
30
id next_objc_msg_sendv(id object, SEL op, void* frame)
451
32
arglist_t argFrame = __builtin_apply_args();
474
55
__builtin_return(result);
478
** Hook functions for memory allocation and disposal.
479
** This makes it easy to substitute garbage collection systems
480
** such as Boehm's GC by assigning these function pointers
481
** to the GC's allocation routines. By default these point
482
** to the ANSI standard malloc, realloc, free, etc.
484
** Users should call the normal objc routines above for
485
** memory allocation and disposal within their programs.
488
void *(*_objc_malloc)(size_t) = malloc;
489
void *(*_objc_atomic_malloc)(size_t) = malloc;
490
void *(*_objc_valloc)(size_t) = malloc;
491
void *(*_objc_realloc)(void *, size_t) = realloc;
492
void *(*_objc_calloc)(size_t, size_t) = calloc;
493
void (*_objc_free)(void *) = free;
496
** Standard functions for memory allocation and disposal.
497
** Users should use these functions in their ObjC programs so
498
** that they work properly with garbage collectors as well as
499
** can take advantage of the exception/error handling available.
503
objc_malloc(size_t size)
505
void* res = (void*) (*_objc_malloc)(size);
507
objc_error(nil, OBJC_ERR_MEMORY, "Virtual memory exhausted\n");
512
objc_atomic_malloc(size_t size)
514
void* res = (void*) (*_objc_atomic_malloc)(size);
516
objc_error(nil, OBJC_ERR_MEMORY, "Virtual memory exhausted\n");
521
objc_valloc(size_t size)
523
void* res = (void*) (*_objc_valloc)(size);
525
objc_error(nil, OBJC_ERR_MEMORY, "Virtual memory exhausted\n");
530
objc_realloc(void *mem, size_t size)
532
void* res = (void*) (*_objc_realloc)(mem, size);
534
objc_error(nil, OBJC_ERR_MEMORY, "Virtual memory exhausted\n");
539
objc_calloc(size_t nelem, size_t size)
541
void* res = (void*) (*_objc_calloc)(nelem, size);
543
objc_error(nil, OBJC_ERR_MEMORY, "Virtual memory exhausted\n");
554
** Error handler function
555
** NULL so that default is to just print to stderr
557
static objc_error_handler _objc_error_handler = NULL;
559
/* Trigger an objc error */
561
objc_error(id object, int code, const char* fmt, ...)
566
objc_verror(object, code, fmt, ap);
570
/* Trigger an objc error */
572
objc_verror(id object, int code, const char* fmt, va_list ap)
576
/* Call the error handler if its there
577
Otherwise print to stderr */
578
if (_objc_error_handler)
579
result = (*_objc_error_handler)(object, code, fmt, ap);
581
vfprintf (stderr, fmt, ap);
583
/* Continue if the error handler says its ok
584
Otherwise abort the program */
591
/* Set the error handler */
593
objc_set_error_handler(objc_error_handler func)
595
objc_error_handler temp = _objc_error_handler;
596
_objc_error_handler = func;