1 // Copyright 2014, ARM Limited
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
7 // * Redistributions of source code must retain the above copyright notice,
8 // this list of conditions and the following disclaimer.
9 // * Redistributions in binary form must reproduce the above copyright notice,
10 // this list of conditions and the following disclaimer in the documentation
11 // and/or other materials provided with the distribution.
12 // * Neither the name of ARM Limited nor the names of its contributors may be
13 // used to endorse or promote products derived from this software without
14 // specific prior written permission.
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS CONTRIBUTORS "AS IS" AND
17 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
20 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #ifndef VIXL_CODE_BUFFER_H
28 #define VIXL_CODE_BUFFER_H
37 explicit CodeBuffer(size_t capacity
= 4 * KBytes
);
38 CodeBuffer(void* buffer
, size_t capacity
);
43 ptrdiff_t OffsetFrom(ptrdiff_t offset
) const {
44 ptrdiff_t cursor_offset
= cursor_
- buffer_
;
45 VIXL_ASSERT((offset
>= 0) && (offset
<= cursor_offset
));
46 return cursor_offset
- offset
;
49 ptrdiff_t CursorOffset() const {
54 T
GetOffsetAddress(ptrdiff_t offset
) const {
55 VIXL_ASSERT((offset
>= 0) && (offset
<= (cursor_
- buffer_
)));
56 return reinterpret_cast<T
>(buffer_
+ offset
);
59 size_t RemainingBytes() const {
60 VIXL_ASSERT((cursor_
>= buffer_
) && (cursor_
<= (buffer_
+ capacity_
)));
61 return (buffer_
+ capacity_
) - cursor_
;
64 // A code buffer can emit:
65 // * 32-bit data: instruction and constant.
66 // * 64-bit data: constant.
67 // * string: debug info.
68 void Emit32(uint32_t data
) { Emit(data
); }
70 void Emit64(uint64_t data
) { Emit(data
); }
72 void EmitString(const char* string
);
74 // Align to kInstructionSize.
77 size_t capacity() const { return capacity_
; }
79 bool IsManaged() const { return managed_
; }
81 void Grow(size_t new_capacity
);
83 bool IsDirty() const { return dirty_
; }
85 void SetClean() { dirty_
= false; }
90 VIXL_ASSERT(RemainingBytes() >= sizeof(value
));
92 memcpy(cursor_
, &value
, sizeof(value
));
93 cursor_
+= sizeof(value
);
96 // Backing store of the buffer.
98 // If true the backing store is allocated and deallocated by the buffer. The
99 // backing store can then grow on demand. If false the backing store is
100 // provided by the user and cannot be resized internally.
102 // Pointer to the next location to be written.
104 // True if there has been any write since the buffer was created or cleaned.
106 // Capacity in bytes of the backing store.
112 #endif // VIXL_CODE_BUFFER_H