I will post here excerpts from my mailings that might be useful to nxweb users.
1. Memory allocation
Is there an allocating/buffering API which would automatically handle free()
at the right time (a la libevent's buffer_events)?
Yes, there is. Look at nx_buffer.h. Both request and response structs have a pointer to the same nxb_buffer; it is called nxb. It gets initialized when request arrives and is automatically freed when request is complete. It is very similar to obstack (from glibc) and can be used in two ways:
1) Object allocation:
void* ptr1=nxb_alloc_obj(req->nxb, 1024);
void* ptr2=nxb_calloc_obj(req->nxb, 256);
void* ptr1copy=nxb_copy_obj(req->nxb, ptr1, 1024);
2) Character streaming (variable length buffer):
nxb_printf(req->nxb, "Test %d\n", 12);
// printf-like functions are very slow, avoid them if you care for speed:
nxb_append_str(req->nxb, "Test ");
nxb_append_uint(req->nxb, 12);
nxb_append_char(req->nxb, '\n');
// or even faster (no checking for room every call):
nxb_make_room(req->nxb, 20);
nxb_append_str_fast(req->nxb, "Test ");
nxb_append_uint(req->nxb, 12);
nxb_append_char_fast(req->nxb, '\n');
// finish the stream:
int result_size;
char* result=nxb_finish_stream(req->nxb, &result_size);
Beware that you can not have more than one unfinished stream per nxb, and all nxweb_response_append_* kind of functions do use the same request's nxb, so if you need to stream some characters in your handler do it and finish your stream before using any of these response construction functions. Whatever is left unfinished in nxb on return from on_request considered to be response body.
As opposed to standard obstack implementation nx_buffer has no restriction on interleaving stream and object allocation calls.
nx_buffer is also the most efficient way to allocate small memory blocks in nxweb. Technically there is no limit on size of objects you allocate through nx_buffer (apart from size parameter being of type int), but I think that huge memory chunks are better allocated in some other ways, which are not defined in nxweb so far.