Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
2.5 KiB

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