Create a custom data type that is a renamed void pointer.
typedef void* myPointerType;
Write an allocate function (that can be a simple wrapper around malloc
) that the user must use to allocate memory.
myPointerType myAllocateFunction (size_t size) {
return (myPointerType)(malloc(size));
}
You would also want to make a matching "free" function.
Now, have your function (the one that performs the realloc
) take a myPointerType
object as a parameter instead of a generic pointer. You can cast it back down to a void*
to use with realloc
. To get around this, the user would have to manually cast a non-malloc
ed pointer to a myPointerType
, but you can specify in your documentation that casting to and from myPointerType
is not allowed (so if they do this and their app crashes, it's because they misused the API).
There are even stronger ways that you can enforce this, but I'm not sure if it would be worth the trouble. You can't completely fool-proof an API (you'd be amazed at how capable fools are these days). Whatever you end up implementing, the best way to ensure that your API is used correctly is to provide clear, comprehensive documentation.
You're on your own for the pony, sorry.