The SDL forums have moved to discourse.libsdl.org.
This is just a read-only archive of the previous forums, to keep old links working.


SDL Forum Index
SDL
Simple Directmedia Layer Forums
SDL_RWOps flexibility
Felipe Oliveira Carvalho
Guest

Hi,


I'm implementing a function to read the contents of a file using SDL_RWOps. I looked at the code and the way one has to initialize a SDL_RWOps context forces a heap memory allocation (SDL_AllocRW) and deallocation (SDL_FreeRW).


I want to avoid the allocation. Any reason for not providing a function that accepts an existing SDL_RWOps pointer and initializes it?

{

   SDL_RWops ops;

   new_SDL_RWFromFile(&ops, filename, "r");
   ....
}


This way I could reuse the SDL_RWOps when dealing with multiple files without allocating/deallocating memory every time.


The current API could be implemented as an special case: pass the SDL_malloc'd SDL_RWOps to new_RWOpsFrom*.


--

Felipe
SDL_RWOps flexibility
Rainer Deyke
Guest

On 08.01.2015 05:17, Felipe Oliveira Carvalho wrote:
Quote:
Hi,

I'm implementing a function to read the contents of a file using SDL_RWOps.
I looked at the code and the way one has to initialize a SDL_RWOps context
forces a heap memory allocation (SDL_AllocRW) and deallocation (SDL_FreeRW).

I want to avoid the allocation. Any reason for not providing a function
that accepts an existing SDL_RWOps pointer and initializes it?

{
SDL_RWops ops;
new_SDL_RWFromFile(&ops, filename, "r");
....
}

This way I could reuse the SDL_RWOps when dealing with multiple files
without allocating/deallocating memory every time.

You do realize that opening a file usually involves several memory
allocations even without SDL_RWOps, right?


--
Rainer Deyke

_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
SDL_RWOps flexibility
Jonny D


Joined: 12 Sep 2009
Posts: 932
My suggestion would be that the RWops interface reflects the way all of the other interfaces in SDL work (see also SDL_CreateRGBSurface(), SDL_CreateWindow(), etc.).  Structures meant to be used as mostly or completely opaque are dynamically allocated and a pointer is returned.  Structures used more like value types (e.g. SDL_Rect) have the flexibility to be on the stack.

SDL might not do as much on the stack as is possible, but it surely is more directly portable to other language bindings.

Jonny D





On Fri, Jan 9, 2015 at 3:06 AM, Rainer Deyke wrote:
Quote:
On 08.01.2015 05:17, Felipe Oliveira Carvalho wrote:
Quote:
Hi,

I'm implementing a function to read the contents of a file using SDL_RWOps.
I looked at the code and the way one has to initialize a SDL_RWOps context
forces a heap memory allocation (SDL_AllocRW) and deallocation (SDL_FreeRW).

I want to avoid the allocation. Any reason for not providing a function
that accepts an existing SDL_RWOps pointer and initializes it?

{
    SDL_RWops ops;
    new_SDL_RWFromFile(&ops, filename, "r");
    ....
}

This way I could reuse the SDL_RWOps when dealing with multiple files
without allocating/deallocating memory every time.

You do realize that opening a file usually involves several memory allocations even without SDL_RWOps, right?


--
Rainer Deyke

_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org