LibC: Add mmap_with_name() that names the allocation immediately.

This allows us to skip the separate call to set_mmap_name() in code that
we control, e.g malloc() and GraphicsBitmap.
This commit is contained in:
Andreas Kling 2019-05-19 15:54:56 +02:00
parent 5f26f83451
commit 189b342e6f
Notes: sideshowbarker 2024-07-19 14:01:04 +09:00
8 changed files with 28 additions and 13 deletions

View file

@ -151,12 +151,15 @@ void* Process::sys$mmap(const Syscall::SC_mmap_params* params)
{
if (!validate_read(params, sizeof(Syscall::SC_mmap_params)))
return (void*)-EFAULT;
if (params->name && !validate_read_str(params->name))
return (void*)-EFAULT;
void* addr = (void*)params->addr;
size_t size = params->size;
int prot = params->prot;
int flags = params->flags;
int fd = params->fd;
off_t offset = params->offset;
const char* name = params->name;
if (size == 0)
return (void*)-EINVAL;
if ((dword)addr & ~PAGE_MASK)
@ -167,6 +170,8 @@ void* Process::sys$mmap(const Syscall::SC_mmap_params* params)
return (void*)-ENOMEM;
if (flags & MAP_SHARED)
region->set_shared(true);
if (name)
region->set_name(name);
return region->laddr().as_ptr();
}
if (offset & ~PAGE_MASK)
@ -180,6 +185,8 @@ void* Process::sys$mmap(const Syscall::SC_mmap_params* params)
auto region = region_or_error.value();
if (flags & MAP_SHARED)
region->set_shared(true);
if (name)
region->set_name(name);
return region->laddr().as_ptr();
}

View file

@ -134,6 +134,7 @@ struct SC_mmap_params {
int32_t flags;
int32_t fd;
int32_t offset; // FIXME: 64-bit off_t?
const char* name { nullptr };
};
struct SC_select_params {

View file

@ -31,8 +31,7 @@ void setgrent()
perror("open /etc/group");
}
assert(__grdb_stream);
__grdb_entry = (struct group_with_strings*)mmap(nullptr, getpagesize(), PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
set_mmap_name(__grdb_entry, getpagesize(), "setgrent");
__grdb_entry = (struct group_with_strings*)mmap_with_name(nullptr, getpagesize(), PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0, "setgrent");
}
}

View file

@ -123,9 +123,9 @@ size_t malloc_good_size(size_t size)
return PAGE_ROUND_UP(size);
}
static void* os_alloc(size_t size)
static void* os_alloc(size_t size, const char* name)
{
return mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
return mmap_with_name(nullptr, size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0, name);
}
static void os_free(void* ptr, size_t size)
@ -155,10 +155,9 @@ void* malloc(size_t size)
}
}
#endif
auto* block = (BigAllocationBlock*)os_alloc(real_size);
char buffer[64];
snprintf(buffer, sizeof(buffer), "malloc: BigAllocationBlock(%u)", real_size);
set_mmap_name(block, PAGE_SIZE, buffer);
auto* block = (BigAllocationBlock*)os_alloc(real_size, buffer);
new (block) BigAllocationBlock(real_size);
return &block->m_slot[0];
}
@ -171,10 +170,9 @@ void* malloc(size_t size)
}
if (!block) {
block = (ChunkedBlock*)os_alloc(PAGE_SIZE);
char buffer[64];
snprintf(buffer, sizeof(buffer), "malloc: ChunkedBlock(%u)", good_size);
set_mmap_name(block, PAGE_SIZE, buffer);
block = (ChunkedBlock*)os_alloc(PAGE_SIZE, buffer);
new (block) ChunkedBlock(good_size);
allocator->usable_blocks.append(block);
++allocator->block_count;

View file

@ -7,7 +7,18 @@ extern "C" {
void* mmap(void* addr, size_t size, int prot, int flags, int fd, off_t offset)
{
Syscall::SC_mmap_params params { (dword)addr, size, prot, flags, fd, offset };
Syscall::SC_mmap_params params { (dword)addr, size, prot, flags, fd, offset, nullptr };
int rc = syscall(SC_mmap, &params);
if (rc < 0 && -rc < EMAXERRNO) {
errno = -rc;
return (void*)-1;
}
return (void*)rc;
}
void* mmap_with_name(void* addr, size_t size, int prot, int flags, int fd, off_t offset, const char* name)
{
Syscall::SC_mmap_params params { (dword)addr, size, prot, flags, fd, offset, name };
int rc = syscall(SC_mmap, &params);
if (rc < 0 && -rc < EMAXERRNO) {
errno = -rc;

View file

@ -19,6 +19,7 @@
__BEGIN_DECLS
void* mmap(void* addr, size_t, int prot, int flags, int fd, off_t);
void* mmap_with_name(void* addr, size_t, int prot, int flags, int fd, off_t, const char* name);
int munmap(void*, size_t);
int set_mmap_name(void*, size_t, const char*);
int shm_open(const char* name, int flags, mode_t);

View file

@ -32,8 +32,7 @@ void setpwent()
perror("open /etc/passwd");
}
assert(__pwdb_stream);
__pwdb_entry = (struct passwd_with_strings*)mmap(nullptr, getpagesize(), PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
set_mmap_name(__pwdb_entry, getpagesize(), "setpwent");
__pwdb_entry = (struct passwd_with_strings*)mmap_with_name(nullptr, getpagesize(), PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0, "setpwent");
}
}

View file

@ -19,10 +19,9 @@ GraphicsBitmap::GraphicsBitmap(Format format, const Size& size)
{
if (format == Format::Indexed8)
m_palette = new RGBA32[256];
m_data = (RGBA32*)mmap(nullptr, size_in_bytes(), PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
m_data = (RGBA32*)mmap_with_name(nullptr, size_in_bytes(), PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0, String::format("GraphicsBitmap [%dx%d]", width(), height()).characters());
ASSERT(m_data && m_data != (void*)-1);
m_needs_munmap = true;
set_mmap_name(String::format("GraphicsBitmap [%dx%d]", width(), height()).characters());
}
Retained<GraphicsBitmap> GraphicsBitmap::create_wrapper(Format format, const Size& size, RGBA32* data)