1
0

bmem.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * Copyright (c) 2013 Hugh Bailey <[email protected]>
  3. *
  4. * Permission to use, copy, modify, and distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #pragma once
  17. #include "c99defs.h"
  18. #include "base.h"
  19. #include <wchar.h>
  20. #include <string.h>
  21. #ifdef __cplusplus
  22. extern "C" {
  23. #endif
  24. struct base_allocator {
  25. void *(*malloc)(size_t);
  26. void *(*realloc)(void *, size_t);
  27. void (*free)(void *);
  28. };
  29. OBS_DEPRECATED EXPORT void base_set_allocator(struct base_allocator *defs);
  30. EXPORT void *bmalloc(size_t size);
  31. EXPORT void *brealloc(void *ptr, size_t size);
  32. EXPORT void bfree(void *ptr);
  33. EXPORT int base_get_alignment(void);
  34. EXPORT long bnum_allocs(void);
  35. EXPORT void *bmemdup(const void *ptr, size_t size);
  36. static inline void *bzalloc(size_t size)
  37. {
  38. void *mem = bmalloc(size);
  39. if (mem)
  40. memset(mem, 0, size);
  41. return mem;
  42. }
  43. static inline char *bstrdup_n(const char *str, size_t n)
  44. {
  45. char *dup;
  46. if (!str)
  47. return NULL;
  48. dup = (char *)bmemdup(str, n + 1);
  49. dup[n] = 0;
  50. return dup;
  51. }
  52. static inline wchar_t *bwstrdup_n(const wchar_t *str, size_t n)
  53. {
  54. wchar_t *dup;
  55. if (!str)
  56. return NULL;
  57. dup = (wchar_t *)bmemdup(str, (n + 1) * sizeof(wchar_t));
  58. dup[n] = 0;
  59. return dup;
  60. }
  61. static inline char *bstrdup(const char *str)
  62. {
  63. if (!str)
  64. return NULL;
  65. return bstrdup_n(str, strlen(str));
  66. }
  67. static inline wchar_t *bwstrdup(const wchar_t *str)
  68. {
  69. if (!str)
  70. return NULL;
  71. return bwstrdup_n(str, wcslen(str));
  72. }
  73. #ifdef __cplusplus
  74. }
  75. #endif