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