|
@@ -37,39 +37,51 @@ namespace dsr {
|
|
|
using Buffer = std::shared_ptr<BufferImpl>;
|
|
using Buffer = std::shared_ptr<BufferImpl>;
|
|
|
|
|
|
|
|
// Creates a new buffer of newSize bytes.
|
|
// Creates a new buffer of newSize bytes.
|
|
|
|
|
+ // Pre-condition: newSize > 0
|
|
|
Buffer buffer_create(int64_t newSize);
|
|
Buffer buffer_create(int64_t newSize);
|
|
|
|
|
|
|
|
// Creates a new buffer of newSize bytes inheriting ownership of newData.
|
|
// Creates a new buffer of newSize bytes inheriting ownership of newData.
|
|
|
// If the given data cannot be freed as a C allocation, replaceDestructor must be called with the special destructor.
|
|
// If the given data cannot be freed as a C allocation, replaceDestructor must be called with the special destructor.
|
|
|
|
|
+ // Pre-condition: newSize > 0
|
|
|
Buffer buffer_create(int64_t newSize, uint8_t *newData);
|
|
Buffer buffer_create(int64_t newSize, uint8_t *newData);
|
|
|
|
|
|
|
|
// Sets the allocation's destructor, to be called when there are no more reference counted pointers to the buffer.
|
|
// Sets the allocation's destructor, to be called when there are no more reference counted pointers to the buffer.
|
|
|
|
|
+ // Pre-condition: buffer exists
|
|
|
void buffer_replaceDestructor(Buffer buffer, const std::function<void(uint8_t *)>& newDestructor);
|
|
void buffer_replaceDestructor(Buffer buffer, const std::function<void(uint8_t *)>& newDestructor);
|
|
|
|
|
|
|
|
- // Returns true iff the handle points to a buffer
|
|
|
|
|
|
|
+ // Returns true iff buffer exists
|
|
|
inline bool buffer_exists(Buffer buffer) {
|
|
inline bool buffer_exists(Buffer buffer) {
|
|
|
return buffer.get() != nullptr;
|
|
return buffer.get() != nullptr;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// Returns a clone of the buffer.
|
|
// Returns a clone of the buffer.
|
|
|
|
|
+ // Giving an empty handle returns an empty handle
|
|
|
Buffer buffer_clone(Buffer buffer);
|
|
Buffer buffer_clone(Buffer buffer);
|
|
|
|
|
|
|
|
// Returns the buffer's size in bytes, as given when allocating it excluding allocation padding
|
|
// Returns the buffer's size in bytes, as given when allocating it excluding allocation padding
|
|
|
|
|
+ // Returns zero if buffer doesn't exist
|
|
|
int64_t buffer_getSize(Buffer buffer);
|
|
int64_t buffer_getSize(Buffer buffer);
|
|
|
|
|
|
|
|
// Returns a raw pointer to the data.
|
|
// Returns a raw pointer to the data.
|
|
|
|
|
+ // An empty handle will return nullptr.
|
|
|
uint8_t* buffer_dangerous_getUnsafeData(Buffer buffer);
|
|
uint8_t* buffer_dangerous_getUnsafeData(Buffer buffer);
|
|
|
|
|
|
|
|
// A wrapper for getting a bound-checked pointer of the correct element type.
|
|
// A wrapper for getting a bound-checked pointer of the correct element type.
|
|
|
// Only cast to trivially packed types with power of two dimensions so that the compiler does not add padding.
|
|
// Only cast to trivially packed types with power of two dimensions so that the compiler does not add padding.
|
|
|
// The name must be an ansi encoded constant literal, because each String contains a Buffer which would cause a cyclic dependency.
|
|
// The name must be an ansi encoded constant literal, because each String contains a Buffer which would cause a cyclic dependency.
|
|
|
|
|
+ // Returns a safe null pointer if buffer does not exist.
|
|
|
template <typename T>
|
|
template <typename T>
|
|
|
SafePointer<T> buffer_getSafeData(Buffer buffer, const char* name) {
|
|
SafePointer<T> buffer_getSafeData(Buffer buffer, const char* name) {
|
|
|
- uint8_t *data = buffer_dangerous_getUnsafeData(buffer);
|
|
|
|
|
- return SafePointer<T>(name, (T*)data, buffer_getSize(buffer), (T*)data);
|
|
|
|
|
|
|
+ if (!buffer_exists(buffer)) {
|
|
|
|
|
+ return SafePointer<T>();
|
|
|
|
|
+ } else {
|
|
|
|
|
+ uint8_t *data = buffer_dangerous_getUnsafeData(buffer);
|
|
|
|
|
+ return SafePointer<T>(name, (T*)data, buffer_getSize(buffer), (T*)data);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// Set all bytes to the same value
|
|
// Set all bytes to the same value
|
|
|
|
|
+ // Pre-condition: buffer exists
|
|
|
void buffer_setBytes(Buffer buffer, uint8_t value);
|
|
void buffer_setBytes(Buffer buffer, uint8_t value);
|
|
|
}
|
|
}
|
|
|
|
|
|