1 /* ----------------------------------------------------------------------------- 2 * cmalloc.swg 3 * 4 * This library file contains macros that can be used to create objects using 5 * the C malloc function. 6 * ----------------------------------------------------------------------------- */ 7 8 %{ 9 #include <stdlib.h> 10 %} 11 12 /* %malloc(TYPE [, NAME = TYPE]) 13 %calloc(TYPE [, NAME = TYPE]) 14 %realloc(TYPE [, NAME = TYPE]) 15 %free(TYPE [, NAME = TYPE]) 16 %allocators(TYPE [,NAME = TYPE]) 17 18 Creates functions for allocating/reallocating memory. 19 20 TYPE *malloc_NAME(size_t nbytes = sizeof(TYPE); 21 TYPE *calloc_NAME(size_t nobj=1, size_t size=sizeof(TYPE)); 22 TYPE *realloc_NAME(TYPE *ptr, size_t nbytes); 23 void free_NAME(TYPE *ptr); 24 25 */ 26 27 %define %malloc(TYPE,NAME...) 28 #if #NAME != "" 29 %rename(malloc_##NAME) ::malloc(size_t nbytes); 30 #else 31 %rename(malloc_##TYPE) ::malloc(size_t nbytes); 32 #endif 33 34 #if #TYPE != "void" 35 %typemap(default) size_t nbytes "$1 = (size_t) sizeof(TYPE);" 36 #endif 37 TYPE *malloc(size_t nbytes); 38 %typemap(default) size_t nbytes; 39 %enddef 40 41 %define %calloc(TYPE,NAME...) 42 #if #NAME != "" 43 %rename(calloc_##NAME) ::calloc(size_t nobj, size_t sz); 44 #else 45 %rename(calloc_##TYPE) ::calloc(size_t nobj, size_t sz); 46 #endif 47 #if #TYPE != "void" 48 %typemap(default) size_t sz "$1 = (size_t) sizeof(TYPE);" 49 #else 50 %typemap(default) size_t sz "$1 = 1;" 51 #endif 52 %typemap(default) size_t nobj "$1 = 1;" 53 TYPE *calloc(size_t nobj, size_t sz); 54 %typemap(default) size_t sz; 55 %typemap(default) size_t nobj; 56 %enddef 57 58 %define %realloc(TYPE,NAME...) 59 %insert("header") { 60 #if #NAME != "" 61 TYPE *realloc_##NAME(TYPE *ptr, size_t nitems) 62 #else 63 TYPE *realloc_##TYPE(TYPE *ptr, size_t nitems) 64 #endif 65 { 66 #if #TYPE != "void" 67 return (TYPE *) realloc(ptr, nitems*sizeof(TYPE)); 68 #else 69 return (TYPE *) realloc(ptr, nitems); 70 #endif 71 } 72 } 73 #if #NAME != "" 74 TYPE *realloc_##NAME(TYPE *ptr, size_t nitems); 75 #else 76 TYPE *realloc_##TYPE(TYPE *ptr, size_t nitems); 77 #endif 78 %enddef 79 80 %define %free(TYPE,NAME...) 81 #if #NAME != "" 82 %rename(free_##NAME) ::free(TYPE *ptr); 83 #else 84 %rename(free_##TYPE) ::free(TYPE *ptr); 85 #endif 86 void free(TYPE *ptr); 87 %enddef 88 89 %define %sizeof(TYPE,NAME...) 90 #if #NAME != "" 91 %constant size_t sizeof_##NAME = sizeof(TYPE); 92 #else 93 %constant size_t sizeof_##TYPE = sizeof(TYPE); 94 #endif 95 %enddef 96 97 %define %allocators(TYPE,NAME...) 98 %malloc(TYPE,NAME) 99 %calloc(TYPE,NAME) 100 %realloc(TYPE,NAME) 101 %free(TYPE,NAME) 102 #if #TYPE != "void" 103 %sizeof(TYPE,NAME) 104 #endif 105 %enddef 106 107 108 109 110 111