Skip to content

Instantly share code, notes, and snippets.

Other posts

When to use a memory pool?

A [memory pool][mp-wiki] is a data structure for allocating fixed-size memory blocks. This definition has stayed same since I learned it around 2010. Although you can find more general definitions via Google, those are the minority.

The APIs of memory pool implementations vary, but they generally look like:

@attractivechaos
attractivechaos / test.c
Created February 5, 2025 05:17
Testing the performance of memory pool
// You need the following three files to compile:
// https://github.com/attractivechaos/klib/blob/master/kavl.h
// https://github.com/attractivechaos/klib/blob/master/kmempool.h
// https://github.com/attractivechaos/klib/blob/master/kmempool.c
// Compile with: gcc -O3 -Wall test.c kmempool.c -o test
// Run with "./test" for raw malloc and "./test m" for memory pool
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>

Other posts

What is an "arena" in memory allocation?

I first leanred about "arena" when I was trying to understand the internal of [glibc malloc][glibc-alloc] around 2010, but I [later realized][trend] the concept is narrowly defined [in][arena1] [other][arena2] [context][arena3]. This blog post explains the difference in definition and the limitations in the so-called "arena allocators" we use today.

Background: why is malloc needed?

@attractivechaos
attractivechaos / 00README.md
Last active November 30, 2024 03:08
Library to parse math expressions

To compile:

gcc -O3 -DKE_MAIN kexpr.c -o kexpr

Examples:

./kexpr "2+5*2"                  # output 12 (simple arithmetic)
./kexpr "2+5*exp(-1)"            # output 3.8394 (builtin functions)
./kexpr "2+5*exp(x+y)" x=-2 y=1  # output 3.8394 (variables)
./kexpr "2^1" # output 3 (bit operation)
@attractivechaos
attractivechaos / khashl.h
Created May 5, 2024 01:35
Hash table interation
/* The MIT License
Copyright (c) 2019-2023 by Attractive Chaos <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
@attractivechaos
attractivechaos / matmul-split.c
Created January 3, 2024 14:23
Alternative matmul with N+1 allocations per matrix
// see also: https://github.com/attractivechaos/plb2/blob/master/src/c/matmul.c
#include <stdio.h>
#include <stdlib.h>
double **mat_alloc(int n_row, int n_col)
{
double **mat, *a;
int i;
mat = (double**)malloc(n_row * sizeof(void*));
/* The MIT License
Copyright (c) 2008, 2011 Attractive Chaos <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
@attractivechaos
attractivechaos / clist.h
Last active January 19, 2020 05:35
Demonstration of a circular doubly linked list
#pragma once // or use the #ifndef guard
#include <stddef.h> // for offsetof()
typedef struct cl_head_s {
struct cl_head_s *prev, *next;
} cl_head_t;
static inline void cl_insert_after(cl_head_t *q, cl_head_t *p)
{ // insert _p_ after _q_; _q_ is in the list
p->prev = q, p->next = q->next, q->next = p, p->next->prev = p;
@attractivechaos
attractivechaos / dlist.h
Created January 19, 2020 02:50
Demonstrating an intrusive doubly linked list
#pragma once // or use the #ifndef guard
#include <stddef.h> // for offsetof()
typedef struct dl_head_s { // this struct can't be hidden
struct dl_head_s *p[2]; // p[0] points the previous record; p[1] points to the next
} dl_head_t;
// Given a pointer to a struct member, get the pointer to the struct
#define dl_container_of(ptr, type, member) ((type*)((char*)(ptr) - offsetof(type, member)))