1 // AsmJit - Machine code generation for C++
2 //
3 //  * Official AsmJit Home Page: https://asmjit.com
4 //  * Official Github Repository: https://github.com/asmjit/asmjit
5 //
6 // Copyright (c) 2008-2020 The AsmJit Authors
7 //
8 // This software is provided 'as-is', without any express or implied
9 // warranty. In no event will the authors be held liable for any damages
10 // arising from the use of this software.
11 //
12 // Permission is granted to anyone to use this software for any purpose,
13 // including commercial applications, and to alter it and redistribute it
14 // freely, subject to the following restrictions:
15 //
16 // 1. The origin of this software must not be misrepresented; you must not
17 //    claim that you wrote the original software. If you use this software
18 //    in a product, an acknowledgment in the product documentation would be
19 //    appreciated but is not required.
20 // 2. Altered source versions must be plainly marked as such, and must not be
21 //    misrepresented as being the original software.
22 // 3. This notice may not be removed or altered from any source distribution.
23 
24 #ifndef ASMJIT_CORE_CODEBUFFER_H_INCLUDED
25 #define ASMJIT_CORE_CODEBUFFER_H_INCLUDED
26 
27 #include "../core/globals.h"
28 
29 ASMJIT_BEGIN_NAMESPACE
30 
31 //! \addtogroup asmjit_core
32 //! \{
33 
34 // ============================================================================
35 // [asmjit::CodeBuffer]
36 // ============================================================================
37 
38 //! Code or data buffer.
39 struct CodeBuffer {
40   //! The content of the buffer (data).
41   uint8_t* _data;
42   //! Number of bytes of `data` used.
43   size_t _size;
44   //! Buffer capacity (in bytes).
45   size_t _capacity;
46   //! Buffer flags.
47   uint32_t _flags;
48 
49   //! Code buffer flags.
50   enum Flags : uint32_t {
51     //! Buffer is external (not allocated by asmjit).
52     kFlagIsExternal = 0x00000001u,
53     //! Buffer is fixed (cannot be reallocated).
54     kFlagIsFixed = 0x00000002u
55   };
56 
57   //! \name Overloaded Operators
58   //! \{
59 
60   //! Returns a referebce to the byte at the given `index`.
61   inline uint8_t& operator[](size_t index) noexcept {
62     ASMJIT_ASSERT(index < _size);
63     return _data[index];
64   }
65   //! \overload
66   inline const uint8_t& operator[](size_t index) const noexcept {
67     ASMJIT_ASSERT(index < _size);
68     return _data[index];
69   }
70 
71   //! \}
72 
73   //! \name Accessors
74   //! \{
75 
76   //! Returns code buffer flags, see \ref Flags.
flagsCodeBuffer77   inline uint32_t flags() const noexcept { return _flags; }
78   //! Tests whether the code buffer has the given `flag` set.
hasFlagCodeBuffer79   inline bool hasFlag(uint32_t flag) const noexcept { return (_flags & flag) != 0; }
80 
81   //! Tests whether this code buffer has a fixed size.
82   //!
83   //! Fixed size means that the code buffer is fixed and cannot grow.
isFixedCodeBuffer84   inline bool isFixed() const noexcept { return hasFlag(kFlagIsFixed); }
85 
86   //! Tests whether the data in this code buffer is external.
87   //!
88   //! External data can only be provided by users, it's never used by AsmJit.
isExternalCodeBuffer89   inline bool isExternal() const noexcept { return hasFlag(kFlagIsExternal); }
90 
91   //! Tests whether the data in this code buffer is allocated (non-null).
isAllocatedCodeBuffer92   inline bool isAllocated() const noexcept { return _data != nullptr; }
93 
94   //! Tests whether the code buffer is empty.
emptyCodeBuffer95   inline bool empty() const noexcept { return !_size; }
96 
97   //! Returns the size of the data.
sizeCodeBuffer98   inline size_t size() const noexcept { return _size; }
99   //! Returns the capacity of the data.
capacityCodeBuffer100   inline size_t capacity() const noexcept { return _capacity; }
101 
102   //! Returns the pointer to the data the buffer references.
dataCodeBuffer103   inline uint8_t* data() noexcept { return _data; }
104   //! \overload
dataCodeBuffer105   inline const uint8_t* data() const noexcept { return _data; }
106 
107   //! \}
108 
109   //! \name Iterators
110   //! \{
111 
beginCodeBuffer112   inline uint8_t* begin() noexcept { return _data; }
beginCodeBuffer113   inline const uint8_t* begin() const noexcept { return _data; }
114 
endCodeBuffer115   inline uint8_t* end() noexcept { return _data + _size; }
endCodeBuffer116   inline const uint8_t* end() const noexcept { return _data + _size; }
117 
118   //! \}
119 };
120 
121 //! \}
122 
123 ASMJIT_END_NAMESPACE
124 
125 #endif // ASMJIT_CORE_CODEBUFFER_H_INCLUDED
126 
127