@@ -9,87 +9,30 @@ and everything is built upon them.
99This reference will try to list them in order for their better
1010understanding.
1111
12- Definitions
13- -----------
14-
15- Godot uses the standard C99 datatypes, such as ``uint8_t ``,
16- ``uint32_t ``, ``int64_t ``, etc. which are nowadays supported by every
17- compiler. Reinventing the wheel for those is not fun, as it makes code
18- more difficult to read.
19-
20- In general, care is not taken to use the most efficient datatype for a
21- given task unless using large structures or arrays. ``int `` is used
22- through most of the code unless necessary. This is done because nowadays
23- every device has at least a 32-bit bus and can do such operations in
24- one cycle. It makes code more readable too.
25-
26- For files or memory sizes, ``size_t `` is used, which is guaranteed to be
27- 64-bit.
28-
29- For Unicode characters, CharType instead of wchar_t is used, because
30- many architectures have 4 bytes long wchar_t, where 2 bytes might be
31- desired. However, by default, this has not been forced and CharType maps
32- directly to wchar_t.
33-
34- - `core/typedefs.h <https://github.com/godotengine/godot/blob/master/core/typedefs.h >`__
35-
36- Memory model
37- ------------
38-
39- PC is a wonderful architecture. Computers often have gigabytes of RAM,
40- terabytes of storage and gigahertz of CPU, and when an application needs
41- more resources the OS will swap out the inactive ones. Other
42- architectures (like mobile or consoles) are in general more limited.
43-
44- The most common memory model is the heap, where an application will
45- request a region of memory, and the underlying OS will try to fit it
46- somewhere and return it. This often works best and is flexible,
47- but over time and with abuse, this can lead to segmentation.
48-
49- Segmentation slowly creates holes that are too small for most common
50- allocations, so that memory is wasted. There is a lot of literature
51- about heap and segmentation, so this topic will not be developed
52- further here. Modern operating systems use paged memory, which helps
53- mitigate the problem of segmentation but doesn't solve it.
54-
55- However, in many studies and tests, it is shown that given enough
56- memory, if the maximum allocation size is below a given threshold in
57- proportion to the maximum heap size and proportion of memory intended to
58- be unused, segmentation will not be a problem over time as it will
59- remain constant. In other words, leave 10-20% of your memory free
60- and perform all small allocations and you are fine.
61-
62- Godot ensures that all objects that can be allocated dynamically are
63- small (less than a few kB at most). But what happens if an allocation is
64- too large (like an image or mesh geometry or large array)? In this case
65- Godot has the option to use a dynamic memory pool. This memory needs to
66- be locked to be accessed, and if an allocation runs out of memory, the
67- pool will be rearranged and compacted on demand. Depending on the need
68- of the game, the programmer can configure the dynamic memory pool size.
69-
7012Allocating memory
7113-----------------
7214
73- Godot has many tools for tracking memory usage in a game, especially
74- during debug . Because of this, the regular C and C++ library calls
75- should not be used. Instead, a few other ones are provided.
15+ Godot has many tricks for ensuring memory safety and tracking memory
16+ usage . Because of this, the regular C and C++ library calls
17+ should not be used. Instead, a few replacements are provided.
7618
7719For C-style allocation, Godot provides a few macros:
7820
79- .. code-block :: none
21+ .. code-block :: cpp
8022
81- memalloc()
82- memrealloc()
83- memfree()
23+ memalloc(size )
24+ memrealloc(pointer )
25+ memfree(pointer )
8426
8527 These are equivalent to the usual ``malloc() ``, ``realloc() ``, and ``free() ``
8628of the C standard library.
8729
8830For C++-style allocation, special macros are provided:
8931
90- .. code-block :: none
32+ .. code-block :: cpp
9133
92- memnew(Class / Class(args))
34+ memnew(Class)
35+ memnew(Class(args))
9336 memdelete(instance)
9437
9538 memnew_arr(Class, amount)
@@ -98,33 +41,9 @@ For C++-style allocation, special macros are provided:
9841 These are equivalent to ``new ``, ``delete ``, ``new[] ``, and ``delete[] ``
9942respectively.
10043
101- memnew/memdelete also use a little C++ magic and notify Objects right
102- after they are created, and right before they are deleted.
103-
104- For dynamic memory, use one of Godot's sequence types such as ``Vector<> ``
105- or ``LocalVector<> ``. ``Vector<> `` behaves much like an STL ``std::vector<> ``,
106- but is simpler and uses Copy-On-Write (CoW) semantics. CoW copies of
107- ``Vector<> `` can safely access the same data from different threads, but
108- several threads cannot access the same ``Vector<> `` instance safely.
109- It can be safely passed via public API if it has a ``Packed `` alias.
110-
111- The ``Packed*Array `` :ref: `types <doc_gdscript_packed_arrays >` are aliases
112- for specific ``Vector<*> `` types (e.g., ``PackedByteArray ``,
113- ``PackedInt32Array ``) that are accessible via GDScript. Outside of core,
114- prefer using the ``Packed*Array `` aliases for functions exposed to scripts,
115- and ``Vector<> `` for other occasions.
116-
117- ``LocalVector<> `` is much more like ``std::vector `` than ``Vector<> ``.
118- It is non-CoW, with less overhead. It is intended for internal use where
119- the benefits of CoW are not needed. Note that neither ``LocalVector<> ``
120- nor ``Vector<> `` are drop-in replacements for each other. They are two
121- unrelated types with similar interfaces, both using a buffer as their
122- storage strategy.
123-
124- ``List<> `` is another Godot sequence type, using a doubly-linked list as
125- its storage strategy. Prefer ``Vector<> `` (or ``LocalVector<> ``) over
126- ``List<> `` unless you're sure you need it, as cache locality and memory
127- fragmentation tend to be more important with small collections.
44+ ``memnew ``/``memdelete `` also use a little C++ magic to automatically call post-init
45+ and pre-release functions. For example, this is used to notify Objects right after
46+ they are created, and right before they are deleted.
12847
12948- `core/os/memory.h <https://github.com/godotengine/godot/blob/master/core/os/memory.h >`__
13049
@@ -231,6 +150,12 @@ scripting API.
231150.. |typed_dictionary | replace :: `TypedDictionary <https://github.com/godotengine/godot/blob/master/core/variant/typed_dictionary.h >`__
232151.. |pair | replace :: `Pair <https://github.com/godotengine/godot/blob/master/core/templates/pair.h >`__
233152
153+ Thread safety
154+ ~~~~~~~~~~~~~
155+
156+ None of Godot's containers are thread-safe. When you expect multiple threads to access them, you must use multithread
157+ protections, like ``Mutex `` or ``Semaphore ``.
158+
234159Math types
235160----------
236161
0 commit comments