Search code examples
clinuxpointersmemory-managementluajit

Malloc specific address or page (specify a "minimum offset") on linux


In LuaJIT on linux, all VM magaged ram has to be below the 2GB process memory boundary because the internal pointers are always 32bit. therefore I want to manage bigger allocs myself (using FFI and malloc,etc), e.g. for big textures, audio, buffers, etc.

I now want to make sure those are mapped above the 2GB boudary, because then they don't take any VM manageable memory away.

is there any way to malloc or maybe mmap(without mapped file, or maybe in SHM) to allocate a pointer specifically above that address? doesn't even have to take up the 2gig, just map my pointer to a higher (=non-32bit) address


Solution

  • Allocate a 2 GB block. If it is located below the limit, allocate another 2 GB (this one must be above 2 GB since only one block that size can fit below 2 GB).

    /* Allocates size bytes at an address higher than address */
    void *HighMalloc(size_t size, void *address) {
        size_t mysize = (size_t)address;
        void *y, *x;
        if (mysize < size) {
            mysize = size;
        }
        y = x = malloc(mysize);
        if (x < address) {
            /* The memory starts at a low address. 
             * mysize is so big that another block this size cannot fit 
             * at a low address. So let's allocate another block and 
             * then free the block that is using low memory. */
            x = malloc(mysize);
            free(y);
        }
        return x;
    }
    

    Note:

    If size is smaller than address, there may be sufficient space at a low address at the second malloc. That is why I increase the allocated size in those cases. So don't use this to allocate small memory chunks. Allocate a big chunk and then divide it into smaller pieces manually.