![]() |
Inside the kernel: memory and containers | ![]() |
Every part of TeXmacs is written with a handful of home-made types: string, array, list, tree, hashmap and a few more, all built on the same memory model. Basic data types introduces their interfaces. This chapter explains how they work inside, because their behaviour follows from it: how objects are allocated and freed, what an assignment shares, how much each operation costs, and which references stay valid. The last page lists the hazards which follow, several of them checked with small programs.
The kernel is not thread-safe: reference counts, the allocator and the global tables are plain variables, so all these objects must only be used from the main thread.
Small objects come from free lists kept per size by fast_alloc (
System/Misc/fast_alloc.hpp
); tm_new and tm_delete replace new and delete.
Almost every type is a handle, a pointer to a reference counted representation, generated by the macros of
Kernel/Abstractions/basic.hpp
. Copying a handle shares the representation; the representation is deleted when its last handle goes.
Strings and arrays are growable buffers, lists are linked cells, trees are labels with an array of children, hash maps are arrays of lists; all are handles.
ASSERT and FAILED throw a C++ exception which the event handlers catch (see fatal errors and crash reports).
System/Misc/fast_alloc.hpp,
fast_alloc.cpp
The allocator and tm_new, tm_delete, tm_new_array, tm_delete_array.
Kernel/Abstractions/basic.hpp,
basic.cpp
Integer types, the handle macros, ASSERT, FAILED, debug flags and the hash functions of numbers and pointers.
Kernel/Types/string.hpp,
Kernel/Containers/array.hpp,
Kernel/Containers/list.hpp
Strings, arrays and lists (the template code is in the .cpp files, which the headers include).
Kernel/Types/tree.hpp,
Kernel/Types/tree_label.hpp
Trees and their labels.
Kernel/Containers/hashmap.hpp,
hashset.hpp,
iterator.hpp,
rel_hashmap.hpp,
hashfunc.hpp,
hashtree.hpp
Hash tables and their relatives.
tests/Kernel
Unit tests of the containers and types (QtTest, run with ctest in a CMake build).