saprykin / plibsys

Highly portable C system library: threads and synchronization primitives, sockets (TCP, UDP, SCTP), IPv4 and IPv6, IPC, hash functions (MD5, SHA-1, SHA-2, SHA-3, GOST), binary trees (RB, AVL) and more. Native code performance.
MIT License
684 stars 75 forks source link

Simplifying plibsys sockets #72

Closed akalsi87 closed 6 years ago

akalsi87 commented 6 years ago

I was looking through the documentation and found that plibsys sockets use an opaque structure and require a malloc call for socket initialization.

I was hoping this could be avoided by providing an alternative definition for the socket which uses a fixed size storage for the fd (which can be a void* on Windows but an int everywhere else) to avoid the use of the memory allocator.

Thoughts?

saprykin commented 6 years ago

What is the purpose to not to call one extra malloc()? I mean, anyway you need to call p_socket_new() in order to initialize internal socket state. Thus I don't see any API simplification. Moreover, on Windows WSACreateEvent() is used when creating a socket, and I'm not sure that it doesn't call any memory allocation routines internally. Also changing behaviour of p_socket_new() to not to return an allocated structure will introduce API inconsistency with other parts of the library. So we need a solid reason to change the behaviour of socket creation.

akalsi87 commented 6 years ago

I do understand API compatibility concerns, but the reasoning at my end is more toward considering the cases where connections are short lived and made and destroyed often. By using malloc internally, I'm prevented from even having my own pool.

There should be a p_socket_init(PSocket *) and then p_socket_new can call it after the malloc.

It's unfortunate to take away the options as far as memory management from the client.

Just my 2c.

saprykin commented 6 years ago

Yes, I do get your point. But please consider the following:

I don't see any added value in removing a single malloc call for the procedure which is not intended to be a performance critical (creating a socket). Of course, it is nice to have a tool which can do everything and everywhere, but this makes this tool overcomplicated to use as well.

akalsi87 commented 6 years ago

Ok. I understand your point of view. Closing.