PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/Lib/typemaps/cmalloc.swg

#
Unknown | 110 lines | 93 code | 17 blank | 0 comment | 0 complexity | 106622913ba988820dcd744eb3720b2a MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  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. #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(size_t nbytes = sizeof(TYPE);
  17. TYPE *calloc_NAME(size_t nobj=1, size_t size=sizeof(TYPE));
  18. TYPE *realloc_NAME(TYPE *ptr, size_t nbytes);
  19. void free_NAME(TYPE *ptr);
  20. */
  21. %define %malloc(TYPE,NAME...)
  22. #if #NAME != ""
  23. %rename(malloc_##NAME) ::malloc(size_t nbytes);
  24. #else
  25. %rename(malloc_##TYPE) ::malloc(size_t nbytes);
  26. #endif
  27. #if #TYPE != "void"
  28. %typemap(default) size_t nbytes "$1 = (size_t) sizeof(TYPE);"
  29. #endif
  30. TYPE *malloc(size_t nbytes);
  31. %typemap(default) size_t nbytes;
  32. %enddef
  33. %define %calloc(TYPE,NAME...)
  34. #if #NAME != ""
  35. %rename(calloc_##NAME) ::calloc(size_t nobj, size_t sz);
  36. #else
  37. %rename(calloc_##TYPE) ::calloc(size_t nobj, size_t sz);
  38. #endif
  39. #if #TYPE != "void"
  40. %typemap(default) size_t sz "$1 = (size_t) sizeof(TYPE);"
  41. #else
  42. %typemap(default) size_t sz "$1 = 1;"
  43. #endif
  44. %typemap(default) size_t nobj "$1 = 1;"
  45. TYPE *calloc(size_t nobj, size_t sz);
  46. %typemap(default) size_t sz;
  47. %typemap(default) size_t nobj;
  48. %enddef
  49. %define %realloc(TYPE,NAME...)
  50. %insert("header") {
  51. #if #NAME != ""
  52. TYPE *realloc_##NAME(TYPE *ptr, size_t nitems)
  53. #else
  54. TYPE *realloc_##TYPE(TYPE *ptr, size_t 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, size_t nitems);
  66. #else
  67. TYPE *realloc_##TYPE(TYPE *ptr, size_t 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 size_t sizeof_##NAME = sizeof(TYPE);
  81. #else
  82. %constant size_t 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