Memory allocation in a named address space


How I can allocate a chunk of memory in a named address space different than #0 (or intrinsic)?

I tried…

#define HEAP_2 attribute((address_space(2)))
#include <stdlib.h>
int main() {
int HEAP_2 *ptr = malloc(10 * sizeof (int));
if (ptr == NULL) {
else {
ptr = NULL;
return (1);

However, I got…

If you need to ask, you're probably misusing address spaces.


Maybe, but, what is the correct way to use the address space?

Thanks in advance,

Juan Carlos

The issue with your example is the use of malloc(). This is a C library function that (according to both LLVM semantics and the WG14 draft for embedded C) always returns a pointer in the main address space. Malloc has no argument specifying the address space. It will either recycle an unused address or request memory from the OS, but without an argument specifying the other address space it has no way of knowing that you want an address anywhere other than the default space.

Setting the attribute on the pointer only means that this pointer may only point to addresses in the specified address space. You must define some other mechanism for allocating memory in these spaces. Typically, for example, a kernel using the existing addresses that map to GS- and FS-relative addresses will have already defined these segments when it initialises the memory map. These may be used, for example, for the kernel's address space, which is mapped into the userspace process's address space, but not visible in ring 3 (or it might do things the other way around, so the GS register points to the userspace process's segment).

Perhaps you should explain what you are actually trying to do? Using different address spaces requires you to have an allocator that handles them and it requires the LLVM back end code to map them to something relevant for the architecture. You can't just say 'this pointer is in address space number 47' and expect that to be meaningful.