Improved struct module

Robin Boerdijk robin.boerdijk at nl.origin-it.com
Fri Oct 15 10:51:37 EDT 1999


Jeremy Hylton <jeremy at cnri.reston.va.us> wrote in message
news:14342.11715.902925.758156 at bitdiddle.cnri.reston.va.us...
> >>>>> "RB" == Robin Boerdijk <robin.boerdijk at nl.origin-it.com> writes:
>
>   RB> I do want to come back on the buffer interface though. I think I
>   RB> have no alternative for using it. How else can I encapsulate a C
>   RB> API function that expects a pre-allocated buffer as a parameter.
>
>   RB> For example, how could I encapsulate the following C function:
>
>   RB>     int GetMessageFromQueue(void* buffer, int bufsize);
>
>   RB> where buffer is an application supplied buffer of size
>   RB> bufsize. The function will return the part of the buffer
>   RB> actually filled. The only one who knows what a reasonable size
>   RB> of the buffer is is the application programmer (who is
>   RB> programming in Python).
>
> This sounds a lot like the recv method on sockets.
>
> The C API to recv is:
>
>      ssize_t recv(int socket, void *buffer, size_t length,
>           int flags);
>
> The Python program calls the recv method on a socket and passes a
> buffer size (call the arg buflen).
>
> The C implementation of the recv method is pretty simple:
> 1. Allocate a string of size buflen.

I would like to prevent this extra memory allocation. If I call
the recv() function a lot of times, I would like to be able to reuse a
pre-allocated buffer to spare me a large number of memory allocations. A
writable string object with a buffer interface would allow me to do just
that.

Robin








More information about the Python-list mailing list