/src/z-virt.c

https://bitbucket.org/ekolis/jackband · C · 149 lines · 53 code · 30 blank · 66 comment · 18 complexity · a07a8821c225d3cf6c8c5ba55423a92f MD5 · raw file

  1. /*
  2. * File: z-virt.c
  3. * Purpose: Memory management routines
  4. *
  5. * Copyright (c) 1997 Ben Harrison.
  6. *
  7. * This work is free software; you can redistribute it and/or modify it
  8. * under the terms of either:
  9. *
  10. * a) the GNU General Public License as published by the Free Software
  11. * Foundation, version 2, or
  12. *
  13. * b) the "Angband licence":
  14. * This software may be copied and distributed for educational, research,
  15. * and not for profit purposes provided that this copyright and statement
  16. * are included in all such copies. Other copyrights may also apply.
  17. */
  18. #include "z-virt.h"
  19. #include "z-util.h"
  20. /*
  21. * Hooks for platform-specific memory allocation.
  22. */
  23. static mem_alloc_hook ralloc_aux;
  24. static mem_free_hook rnfree_aux;
  25. static mem_realloc_hook realloc_aux;
  26. /*
  27. * Set the hooks for the memory system.
  28. */
  29. bool mem_set_hooks(mem_alloc_hook alloc, mem_free_hook free, mem_realloc_hook realloc)
  30. {
  31. /* Error-check */
  32. if (!alloc || !free || !realloc) return FALSE;
  33. /* Set up hooks */
  34. ralloc_aux = alloc;
  35. rnfree_aux = free;
  36. realloc_aux = realloc;
  37. return TRUE;
  38. }
  39. /*
  40. * Allocate `len` bytes of memory.
  41. *
  42. * Returns:
  43. * - NULL if `len` == 0; or
  44. * - a pointer to a block of memory of at least `len` bytes
  45. *
  46. * Doesn't return on out of memory.
  47. */
  48. void *mem_alloc(size_t len)
  49. {
  50. void *mem;
  51. /* Allow allocation of "zero bytes" */
  52. if (len == 0) return (NULL);
  53. /* Allocate some memory */
  54. if (ralloc_aux) mem = (*ralloc_aux)(len);
  55. else mem = malloc(len);
  56. /* Handle OOM */
  57. if (!mem) quit("Out of Memory!");
  58. return mem;
  59. }
  60. /*
  61. * Free the memory pointed to by `p`.
  62. *
  63. * Returns NULL.
  64. */
  65. void *mem_free(void *p)
  66. {
  67. /* Easy to free nothing */
  68. if (!p) return (NULL);
  69. /* Free memory */
  70. if (rnfree_aux) (*rnfree_aux)(p);
  71. else free(p);
  72. /* Done */
  73. return (NULL);
  74. }
  75. /*
  76. * Allocate `len` bytes of memory, copying whatever is in `p` with it.
  77. *
  78. * Returns:
  79. * - NULL if `len` == 0 or `p` is NULL; or
  80. * - a pointer to a block of memory of at least `len` bytes
  81. *
  82. * Doesn't return on out of memory.
  83. */
  84. void *mem_realloc(void *p, size_t len)
  85. {
  86. void *mem;
  87. /* Fail gracefully */
  88. if (!p || len == 0) return (NULL);
  89. if (realloc_aux) mem = (*realloc_aux)(p, len);
  90. else mem = realloc(p, len);
  91. /* Handle OOM */
  92. if (!mem) quit("Out of Memory!");
  93. return mem;
  94. }
  95. /*
  96. * Duplicates an existing string `str`, allocating as much memory as necessary.
  97. */
  98. char *string_make(const char *str)
  99. {
  100. char *res;
  101. size_t siz;
  102. /* Error-checking */
  103. if (!str) return NULL;
  104. /* Allocate space for the string (including terminator) */
  105. siz = strlen(str) + 1;
  106. res = mem_alloc(siz);
  107. /* Copy the string (with terminator) */
  108. my_strcpy(res, str, siz);
  109. return res;
  110. }
  111. /*
  112. * Un-allocate a string allocated above.
  113. */
  114. #undef string_free
  115. char *string_free(char *str)
  116. {
  117. /* Kill the buffer of chars we must have allocated above */
  118. return mem_free(str);
  119. }