[libuv] Is it safe to use static or pre-allocated buffer when receiving data form a single loop?

2016-09-21 Thread Sunny
Hi everyone, I'm working on a simple network application and I'm new to the 
libuv, so I have this noob question to ask

If I have an application which use only one loop, with many tcp/udp handler 
registered on it, is it safe to use static buffer in uv_alloc_cb like this?

static char buffer[65535] = {0};

void alloc_buffer(uv_handle_t *handle, size_t suggested_size, uv_buf_t 
*buf) {
  buf->base = (char*)buffer;
  buf->len = 65535;
}

In my imagine, the event loop of libuv would work in a SINGLE thread with 
the following way:
1. System polled an event
2. Invoke uv_alloc_cb
3. Invoke uv_recv_cb with the allocated buffer and other infomation
4. After #3 ends, back to #1

In this circuit, it's easy to point out that the allocated memory will be 
only used in uv_recv_cb and will not been overwritten before #3 ends, so in 
this situation I think use a static buffer here is possible, but I don't 
know whether the process I imagine is correct.

If my imagine is correct, is it safe to use the static buffer?

More questions if it's safe:
1. Does libuv has same behaviour on different platform?
2. Will thread-based handler (file or something else) change the behaviour?

Thanks!!! 

-- 
You received this message because you are subscribed to the Google Groups 
"libuv" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to libuv+unsubscr...@googlegroups.com.
To post to this group, send email to libuv@googlegroups.com.
Visit this group at https://groups.google.com/group/libuv.
For more options, visit https://groups.google.com/d/optout.


[libuv] Is it safe for use a static or pre-allocated buffer when receiving data from loop

2016-09-21 Thread Sunny
Hi, everyone, I'm working on a simple network application and I'm new to 
libuv, so I have a question about the buffer allocation.

If I have only one event loop in application, and I registered many tcp and 
udp handlers on the event loop, does it safe for use the following code as 
uv_alloc_cb?

static char buffer[65535] = {0};

void alloc_buffer(uv_handle_t *handle, size_t suggested_size, uv_buf_t 
*buf) {
  buf->base = (char*)buffer;
  buf->len = 65535;
}

In my imagine, the libuv will work like this in a single thread:
system polled --> call allocator --> call uv_recv_cb --> anthoer system 
polled --> ...,
so the memory allocated in uv_alloc_cb will only been used in the 
uv_recv_cb and will not been overwrite before uv_recv_cb end, it seems safe 
to use static memory in the loop in such situation, but I don't know 
whether libuv do it actually like this.

And more question if libuv actually work like this:
Will it have different behaviour in different platform?
Will the behaviour changes in some special situation(like thread-based 
handler been added to loop)?

Thanks!

-- 
You received this message because you are subscribed to the Google Groups 
"libuv" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to libuv+unsubscr...@googlegroups.com.
To post to this group, send email to libuv@googlegroups.com.
Visit this group at https://groups.google.com/group/libuv.
For more options, visit https://groups.google.com/d/optout.