callbacks.c 902 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /**
  2. @file callbacks.c
  3. @brief ENet callback functions
  4. */
  5. #define ENET_BUILDING_LIB 1
  6. #include "enet/enet.h"
  7. static ENetCallbacks callbacks = { malloc, free, abort };
  8. int
  9. enet_initialize_with_callbacks (ENetVersion version, const ENetCallbacks * inits)
  10. {
  11. if (version < ENET_VERSION_CREATE (1, 3, 0))
  12. return -1;
  13. if (inits -> malloc != NULL || inits -> free != NULL)
  14. {
  15. if (inits -> malloc == NULL || inits -> free == NULL)
  16. return -1;
  17. callbacks.malloc = inits -> malloc;
  18. callbacks.free = inits -> free;
  19. }
  20. if (inits -> no_memory != NULL)
  21. callbacks.no_memory = inits -> no_memory;
  22. return enet_initialize ();
  23. }
  24. void *
  25. enet_malloc (size_t size)
  26. {
  27. void * memory = callbacks.malloc (size);
  28. if (memory == NULL)
  29. callbacks.no_memory ();
  30. return memory;
  31. }
  32. void
  33. enet_free (void * memory)
  34. {
  35. callbacks.free (memory);
  36. }