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.

131 lines
2.5 KiB

10 years ago
  1. /* -*- coding: utf-8 -*-
  2. * ----------------------------------------------------------------------
  3. * Copyright © 2011, RedJack, LLC.
  4. * All rights reserved.
  5. *
  6. * Please see the COPYING file in this distribution for license
  7. * details.
  8. * ----------------------------------------------------------------------
  9. */
  10. #include <stdlib.h>
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include "libcork/core/allocator.h"
  14. #include "libcork/core/attributes.h"
  15. #include "libcork/core/error.h"
  16. #include "libcork/core/types.h"
  17. /*-----------------------------------------------------------------------
  18. * reallocf
  19. */
  20. #if !CORK_HAVE_REALLOCF
  21. void *
  22. cork_xrealloc(void *ptr, size_t new_size)
  23. {
  24. void *result = realloc(ptr, new_size);
  25. if (result == NULL) {
  26. free(ptr);
  27. }
  28. return result;
  29. }
  30. #endif
  31. /*-----------------------------------------------------------------------
  32. * Allocating strings
  33. */
  34. static inline const char *
  35. strndup_internal(const char *str, size_t len)
  36. {
  37. size_t allocated_size = len + sizeof(size_t) + 1;
  38. size_t *new_str = malloc(allocated_size);
  39. if (new_str == NULL) {
  40. return NULL;
  41. }
  42. *new_str = allocated_size;
  43. char *dest = (char *) (void *) (new_str + 1);
  44. strncpy(dest, str, len);
  45. dest[len] = '\0';
  46. return dest;
  47. }
  48. const char *
  49. cork_xstrndup(const char *str, size_t len)
  50. {
  51. return strndup_internal(str, len);
  52. }
  53. const char *
  54. cork_xstrdup(const char *str)
  55. {
  56. return strndup_internal(str, strlen(str));
  57. }
  58. void
  59. cork_strfree(const char *str)
  60. {
  61. size_t *base = ((size_t *) str) - 1;
  62. free(base);
  63. }
  64. /*-----------------------------------------------------------------------
  65. * Abort on failure
  66. */
  67. void *
  68. cork_malloc(size_t size)
  69. {
  70. void *result = cork_xmalloc(size);
  71. if (CORK_UNLIKELY(result == NULL)) {
  72. abort();
  73. }
  74. return result;
  75. }
  76. void *
  77. cork_calloc(size_t count, size_t size)
  78. {
  79. void *result = cork_xcalloc(count, size);
  80. if (CORK_UNLIKELY(result == NULL)) {
  81. abort();
  82. }
  83. return result;
  84. }
  85. void *
  86. cork_realloc(void *ptr, size_t new_size)
  87. {
  88. void *result = cork_xrealloc(ptr, new_size);
  89. if (CORK_UNLIKELY(result == NULL)) {
  90. abort();
  91. }
  92. return result;
  93. }
  94. const char *
  95. cork_strdup(const char *src)
  96. {
  97. const char *result = cork_xstrdup(src);
  98. if (CORK_UNLIKELY(result == NULL)) {
  99. abort();
  100. }
  101. return result;
  102. }
  103. const char *
  104. cork_strndup(const char *src, size_t size)
  105. {
  106. const char *result = cork_xstrndup(src, size);
  107. if (CORK_UNLIKELY(result == NULL)) {
  108. abort();
  109. }
  110. return result;
  111. }