34#define DATA_BLOCK_SIZE 4096
43 const Data *
this = (
const Data *) self;
77 const Range range = { 0, this->length };
97 if (this->length == that->
length) {
98 if (this->length == 0) {
102 return memcmp(this->bytes, that->
bytes, this->length) == 0;
155 mem = malloc(length);
158 memcpy(mem, bytes, length);
174 self->capacity = length;
188 FILE *file = fopen(path,
"rb");
192 int err = fseek(file, 0, SEEK_END);
195 const size_t length = ftell(file);
198 mem = malloc(length);
201 err = fseek(file, 0, SEEK_SET);
204 const size_t read = fread(mem, length, 1, file);
237 FILE *file = fopen(path,
"w");
243 count = fwrite(self->
bytes, self->
length, 1, file);
256#pragma mark - Data mutation
264 const size_t oldLength = self->
length;
269 memcpy(self->
bytes + oldLength, bytes, length);
318 self->capacity = capacity;
319 if (self->capacity) {
321 self->
bytes = calloc(capacity,
sizeof(uint8_t));
352 if (newCapacity > self->capacity) {
354 if (self->
bytes == NULL) {
355 self->
bytes = calloc(newCapacity,
sizeof(uint8_t));
359 uint8_t *owned = malloc(newCapacity);
362 if (length > self->
length) {
368 self->
bytes = realloc(self->
bytes, newCapacity);
370 if (length > self->
length) {
376 self->capacity = newCapacity;
377 }
else if (length > self->
length) {
384#pragma mark - Class lifecycle
427 .instanceSize =
sizeof(
Data),
428 .interfaceOffset = offsetof(
Data, interface),
429 .interfaceSize =
sizeof(DataInterface),
static void destroy(Class *clazz)
ident release(ident obj)
Atomically decrement the given Object's reference count. If the resulting reference count is 0,...
Class * _initialize(const ClassDef *def)
Initializes the given Class.
#define alloc(type)
Allocate and initialize and instance of type.
#define super(type, obj, method,...)
static Data * initWithBytes(Data *self, const uint8_t *bytes, size_t length)
static void setLength(Data *self, size_t length)
static bool writeToFile(const Data *self, const char *path)
static bool isEqual(const Object *self, const Object *other)
static Data * dataWithConstMemory(const ident mem, size_t length)
static void appendData(Data *self, const Data *data)
static Data * initWithCapacity(Data *self, size_t capacity)
static Data * init(Data *self)
static Data * initWithContentsOfFile(Data *self, const char *path)
static void dealloc(Object *self)
static void appendBytes(Data *self, const uint8_t *bytes, size_t length)
static Data * dataWithContentsOfFile(const char *path)
static Object * copy(const Object *self)
static void initialize(Class *clazz)
static Data * initWithData(Data *self, const Data *data)
static Data * dataWithCapacity(size_t capacity)
static Data * dataWithMemory(ident mem, size_t length)
static Data * dataWithBytes(const uint8_t *bytes, size_t length)
static int hash(const Object *self)
static Data * initWithConstMemory(Data *self, const ident mem, size_t length)
static Data * initWithMemory(Data *self, ident mem, size_t length)
int HashForInteger(int hash, const long integer)
Accumulates the hash value of integer into hash.
int HashForBytes(int hash, const uint8_t *bytes, const Range range)
Accumulates the hash value of bytes into hash.
Utilities for calculating hash values.
#define HASH_SEED
The hash seed value.
static bool isKindOfClass(const Object *self, const Class *clazz)
void * ident
The identity type, similar to Objective-C id.
#define do_once(once, block)
Executes the given block at most one time.
ClassDefs are passed to _initialize via an archetype to initialize a Class.
The runtime representation of a Class.
ident interface
The interface of the Class.
Data * initWithContentsOfFile(Data *self, const char *path)
Initializes this Data with the contents of the file at path.
Data * initWithMemory(Data *self, ident mem, size_t length)
Initializes this Data, taking ownership of the specified memory.
Data * initWithData(Data *self, const Data *data)
Initializes this Data with the contents of data.
Data * init(Data *self)
Initializes this Data with length 0.
DataDestructor destroy
An optional destructor that, if set, is called on dealloc.
Data * dataWithMemory(ident mem, size_t length)
Returns a new Data, taking ownership of the specified memory.
Data * dataWithContentsOfFile(const char *path)
Returns a new Data with the contents of the file at path.
void appendBytes(Data *self, const uint8_t *bytes, size_t length)
Appends the given bytes to this Data.
size_t length
The length of bytes.
uint8_t * bytes
The bytes.
Data * initWithCapacity(Data *self, size_t capacity)
Initializes this Data with the given capacity.
void setLength(Data *self, size_t length)
Sets the length of this Data, truncating or expanding it.
Data * initWithConstMemory(Data *self, const ident mem, size_t length)
Initializes this Data with the given const memory.
Data * initWithBytes(Data *self, const uint8_t *bytes, size_t length)
Initializes this Data by copying length of bytes.
Data * dataWithConstMemory(const ident mem, size_t length)
Returns a new Data, backed by the given const memory.
Data * dataWithCapacity(size_t capacity)
Returns a new Data with the given capacity.
Object is the root Class of The Objectively Class hierarchy.
void dealloc(Object *self)
Frees all resources held by this Object.
A location and length into contiguous collections.