Implement malloc, free and realloc functions efficiently