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.

74 lines
3.2 KiB

  1. #ifndef MEMORY_H
  2. #define MEMORY_H
  3. #ident "Copyright (c) 2007 Tokutek Inc. All rights reserved."
  4. //#include <stdlib.h>
  5. /* Tokutek memory allocation functions and macros.
  6. * These are functions for malloc and free */
  7. /* Generally: errno is set to 0 or a value to indicate problems. */
  8. /* Everything should call toku_malloc() instead of malloc(), and toku_calloc() instead of calloc() */
  9. void *toku_calloc(long nmemb, long size);
  10. void *toku_malloc(unsigned long size);
  11. /* toku_tagmalloc() performs a malloc(size), but fills in the first 4 bytes with typ.
  12. * This "tag" is useful if you are debugging and run across a void* that is
  13. * really a (struct foo *), and you want to figure out what it is.
  14. */
  15. void *toku_tagmalloc(unsigned long size, int typ);
  16. void toku_free(void*);
  17. /* toku_free_n() should be used if the caller knows the size of the malloc'd object. */
  18. void toku_free_n(void*, unsigned long size);
  19. void *toku_realloc(void *, long size);
  20. /* MALLOC is a macro that helps avoid a common error:
  21. * Suppose I write
  22. * struct foo *x = malloc(sizeof(struct foo));
  23. * That works fine. But if I change it to this, I've probably made an mistake:
  24. * struct foo *x = malloc(sizeof(struct bar));
  25. * It can get worse, since one might have something like
  26. * struct foo *x = malloc(sizeof(struct foo *))
  27. * which looks reasonable, but it allocoates enough to hold a pointer instead of the amount needed for the struct.
  28. * So instead, write
  29. * struct foo *MALLOC(x);
  30. * and you cannot go wrong.
  31. */
  32. #define MALLOC(v) v = toku_malloc(sizeof(*v))
  33. /* MALLOC_N is like calloc: It makes an array. Write
  34. * int *MALLOC_N(5,x);
  35. * to make an array of 5 integers.
  36. */
  37. #define MALLOC_N(n,v) v = toku_malloc((n)*sizeof(*v))
  38. /* If you have a type such as
  39. * struct pma *PMA;
  40. * and you define a corresponding int constant, such as
  41. * enum typ_tag { TYP_PMA };
  42. * then if you do
  43. * TAGMALLOC(PMA,v);
  44. * you declare a variable v of type PMA and malloc a struct pma, and fill
  45. * in that "tag" with toku_tagmalloc().
  46. */
  47. #define TAGMALLOC(t,v) t v = toku_tagmalloc(sizeof(*v), TYP_ ## t);
  48. /* Copy memory. Analogous to strdup() */
  49. void *toku_memdup (const void *v, unsigned int len);
  50. /* Toku-version of strdup. Use this so that it calls toku_malloc() */
  51. char *toku_strdup (const char *s);
  52. void toku_malloc_cleanup (void); /* Before exiting, call this function to free up any internal data structures from toku_malloc. Otherwise valgrind will complain of memory leaks. */
  53. /* Check to see if everything malloc'd was free. Might be a no-op depending on how memory.c is configured. */
  54. void toku_memory_check_all_free (void);
  55. /* Check to see if memory is "sane". Might be a no-op. Probably better to simply use valgrind. */
  56. void toku_do_memory_check(void);
  57. extern int toku_memory_check; // Set to nonzero to get a (much) slower version of malloc that does (much) more checking.
  58. int toku_get_n_items_malloced(void); /* How many items are malloc'd but not free'd. May return 0 depending on the configuration of memory.c */
  59. void toku_print_malloced_items(void); /* Try to print some malloced-but-not-freed items. May be a noop. */
  60. void toku_malloc_report (void); /* report on statistics about number of mallocs. Maybe a no-op. */
  61. #endif