custom_memory.c 983 B

1234567891011121314151617181920212223242526272829303132333435
  1. # include "custom_memory.h"
  2. # include <stdlib.h>
  3. # include <stdio.h>
  4. //Make a global counter and track the net number of allocations
  5. //by user defined allocators. Should go to zero on exit if no leaks.
  6. long int alloc_counter = 0;
  7. void* my_malloc(size_t size) {
  8. void *m = malloc(size);
  9. alloc_counter++;
  10. /* printf("OSQP allocator (malloc): %zu bytes, %ld allocations \n",size, alloc_counter); */
  11. return m;
  12. }
  13. void* my_calloc(size_t num, size_t size) {
  14. void *m = calloc(num, size);
  15. alloc_counter++;
  16. /* printf("OSQP allocator (calloc): %zu bytes, %ld allocations \n",num*size, alloc_counter); */
  17. return m;
  18. }
  19. void* my_realloc(void *ptr, size_t size) {
  20. void *m = realloc(ptr,size);
  21. /* printf("OSQP allocator (realloc) : %zu bytes, %ld allocations \n",size, alloc_counter); */
  22. return m;
  23. }
  24. void my_free(void *ptr) {
  25. if(ptr != NULL){
  26. free(ptr);
  27. alloc_counter--;
  28. /* printf("OSQP allocator (free) : %ld allocations \n", alloc_counter); */
  29. }
  30. }