cmalloc.i 2.3 KB

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