pocketlang/cli/utils.h
Derick Alangi 04c333a15e
Set pointer to first byte of mem block when realloc()'d (#48)
* While reading various files in the repo, just going ahead to
clean up some typos (for clarity of text).

* When calling `realloc()`, let the pointer be returned to the
first byte of the memory block after resize. Compiler warns against
not doing this too.

* In addition, rename the file uitls.c to "utils.c" which seems to
be the correct name in this case.

NOTE: I recompiled and tested the `./pocket` intepreter and it still
works as expected.
2021-06-10 04:10:24 +05:30

39 lines
1.3 KiB
C

/*
* Copyright (c) 2020-2021 Thakee Nathees
* Distributed Under The MIT License
*/
#include "common.h"
typedef struct {
uint8_t* data;
uint32_t count;
uint32_t capacity;
} ByteBuffer;
/*****************************************************************************/
/* BYTE BUFFER */
/*****************************************************************************/
// Initialize a new buffer int instance.
void byteBufferInit(ByteBuffer* buffer);
// Clears the allocated elements from the VM's realloc function.
void byteBufferClear(ByteBuffer* buffer);
// Ensure the capacity is greater than [size], if not resize.
void byteBufferReserve(ByteBuffer* buffer, size_t size);
// Fill the buffer at the end of it with provided data if the capacity
// isn't enough using VM's realloc function.
void byteBufferFill(ByteBuffer* buffer, uint8_t data, int count);
// Write to the buffer with provided data at the end of the buffer.
void byteBufferWrite(ByteBuffer* buffer, uint8_t data);
// Add all the characters to the buffer, byte buffer can also be used as a
// buffer to write string (like a string stream). Note that this will not
// add a null byte '\0' at the end.
void byteBufferAddString(ByteBuffer* buffer, const char* str, uint32_t length);