allocator.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /* Memory allocators such as malloc+free.
  2. Copyright (C) 2011 Free Software Foundation, Inc.
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. /* Written by Paul Eggert. */
  14. #ifndef _GL_ALLOCATOR_H
  15. #define _GL_ALLOCATOR_H
  16. #include <stddef.h>
  17. /* An object describing a memory allocator family. */
  18. struct allocator
  19. {
  20. /* Do not use GCC attributes such as __attribute__ ((malloc)) with
  21. the function types pointed at by these members, because these
  22. attributes do not work with pointers to functions. See
  23. <http://lists.gnu.org/archive/html/bug-gnulib/2011-04/msg00007.html>. */
  24. /* Call ALLOCATE to allocate memory, like 'malloc'. On failure ALLOCATE
  25. should return NULL, though not necessarily set errno. When given
  26. a zero size it may return NULL even if successful. */
  27. void *(*allocate) (size_t);
  28. /* If nonnull, call REALLOCATE to reallocate memory, like 'realloc'.
  29. On failure REALLOCATE should return NULL, though not necessarily set
  30. errno. When given a zero size it may return NULL even if
  31. successful. */
  32. void *(*reallocate) (void *, size_t);
  33. /* Call FREE to free memory, like 'free'. */
  34. void (*free) (void *);
  35. /* If nonnull, call DIE (SIZE) if MALLOC (SIZE) or REALLOC (...,
  36. SIZE) fails. DIE should not return. SIZE should equal SIZE_MAX
  37. if size_t overflow was detected while calculating sizes to be
  38. passed to MALLOC or REALLOC. */
  39. void (*die) (size_t);
  40. };
  41. /* An allocator using the stdlib functions and a null DIE function. */
  42. extern struct allocator const stdlib_allocator;
  43. #endif /* _GL_ALLOCATOR_H */