callbacks.c 911 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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, rand };
  8. int
  9. enet_initialize_with_callbacks (ENetVersion version, const ENetCallbacks * inits)
  10. {
  11. if (version != ENET_VERSION)
  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 -> rand != NULL)
  21. callbacks.rand = inits -> rand;
  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. abort ();
  30. return memory;
  31. }
  32. void
  33. enet_free (void * memory)
  34. {
  35. callbacks.free (memory);
  36. }
  37. int
  38. enet_rand (void)
  39. {
  40. return callbacks.rand ();
  41. }