GNU TeXmacsUser manualReferenceAboutEnglish
Memory and handles

1.The allocator

tm_new<C> (args) obtains memory with fast_new and constructs a C in it with placement new; tm_delete (p) calls the destructor and gives the memory back with fast_delete (

fast_alloc.hpp:375

). The allocator (

fast_alloc.cpp

) works as follows:

With DEBUG_ON, three words are kept in front of each object (the size twice and its complement) and a guard value after it, and fast_delete reports size mismatches and buffer overflows. With NO_FAST_ALLOC, tm_new and friends are plain new and delete; this is the configuration to use with AddressSanitizer or Valgrind, which cannot see inside the free lists. The X11 port instead replaces the global new and delete by the fast allocator, and tm_new is plain new there too.

mem_used (texmacs-memory in Scheme) adds the large blocks and the used part of the chunks; mem_info prints these numbers. Both use int and overflow beyond 2 GB.

2.Handles and reference counts

A type T is usually a handle: a class with a single member, T_rep* rep, generated by the macros of

basic.hpp

. The representation class derives from concrete_struct or abstract_struct, which contain the reference count. The macros define the copy constructor and the assignment, which increment the count of the new representation and decrement that of the old one (INC_COUNT, DEC_COUNT), the destructor, which decrements and deletes the representation when the count reaches 0, and operator->, which gives access to the members of the representation.

CONCRETE (T), CONCRETE_CODE (T)

The representation is a concrete class, created by the constructors of the handle: T (args): rep (tm_new<T_rep> (args)) {}. A concrete_struct starts with a count of 1, which belongs to the handle that created it.

ABSTRACT (T), ABSTRACT_CODE (T)

The representation is an abstract base class whose subclasses implement the behaviour (widgets, commands, boxes, observers, …; renderers, on the other hand, are plain pointers to renderer_rep). The handle has a constructor T (T_rep*) which increments the count; an abstract_struct therefore starts with a count of 0, and functions return tm_new<sub_rep> (…) as a T. Code which builds such an object without that constructor sets the count itself (the patch constructors in

patch.cpp

write rep->ref_count= 1).

CONCRETE_NULL, ABSTRACT_NULL

The same, but the representation may be NULL: the default constructor makes a nil handle, tested with is_nil. Lists, observers, commands and widgets are of this kind.

…_TEMPLATE, …_TEMPLATE_2

The same for templates with one or two parameters (array<T>, hashmap<T,U>).

tree does not use the macros: its representation can be one of three classes and is deleted by destroy_tree_rep according to its label (

tree.cpp:24

), but the principle is the same.

Consequences of this model:

3.Errors

ASSERT (cond, msg) and FAILED (msg) call tm_throw, since USE_EXCEPTIONS is always defined in

basic.hpp

. tm_throw prints a crash report with a stack trace on the standard output and throws the message as a string. The handlers of key presses, mouse events and menu actions catch it, cancel the changes of the action (see the undo system) and report the error with handle_exceptions; a failure while typesetting replaces the document by an empty one (

edit_typeset.cpp:983

). The whole mechanism is described in fatal errors and crash reports. Assertions are not compiled out in release builds, and they are not cheap: each one that fails builds a full report.

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; with no Invariant Sections, with no Front-Cover Texts, and with no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation License".