jeudi 30 avril 2015

Writing API: what a handle should be

Let's suppose we're writing a library implementing some API. The lib creates some resources (connections/internal data structures/whatever) and returns a handle which then is used to make further calls.

What are recommended ways to represent a handle?

  • Should it be a raw pointer to some internal data structure allocated while creating a session?

  • If it is the case, how do I secure the lib in case API functions are called after close method which deallocates the data handle has pointed to?

If handle is an abstract number (like key in "handle"->"obj" map), won't it affect performance?

What other approaches are possible? Which one should I choose?

If anybody has worked on API's, could you share your experience on the subject?

The question is too wide and abstract, but currently I'm working on very similar task and just don't know how to tackle the problem.

I've been looking through sqlite3 sources, their handle is just a pointer to dynamically allocated structure, they set some magic numbers while opening connection and closing it and checking validity of a handle consists in checking those magic numbers. Is it safe enough?

Aucun commentaire:

Enregistrer un commentaire