From: Jason Ekstrand Date: Wed, 11 Oct 2017 16:54:55 +0000 (-0700) Subject: compiler: Move blob up a level X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=0e3bd56c6ea783dbc;p=mesa.git compiler: Move blob up a level We're going to want to use the blob for Vulkan pipeline caching so it makes sense to have it in libcompiler not libglsl. Reviewed-by: Nicolai Hähnle Reviewed-by: Jordan Justen --- diff --git a/src/compiler/Makefile.sources b/src/compiler/Makefile.sources index 352631a75ad..2724a41286e 100644 --- a/src/compiler/Makefile.sources +++ b/src/compiler/Makefile.sources @@ -1,4 +1,6 @@ LIBCOMPILER_FILES = \ + blob.c \ + blob.h \ builtin_type_macros.h \ glsl_types.cpp \ glsl_types.h \ @@ -17,8 +19,6 @@ LIBGLSL_FILES = \ glsl/ast_function.cpp \ glsl/ast_to_hir.cpp \ glsl/ast_type.cpp \ - glsl/blob.c \ - glsl/blob.h \ glsl/builtin_functions.cpp \ glsl/builtin_functions.h \ glsl/builtin_int64.h \ diff --git a/src/compiler/blob.c b/src/compiler/blob.c new file mode 100644 index 00000000000..65e1376e160 --- /dev/null +++ b/src/compiler/blob.c @@ -0,0 +1,344 @@ +/* + * Copyright © 2014 Intel Corporation + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +#include + +#include "main/macros.h" +#include "blob.h" + +#ifdef HAVE_VALGRIND +#include +#include +#define VG(x) x +#else +#define VG(x) +#endif + +#define BLOB_INITIAL_SIZE 4096 + +/* Ensure that \blob will be able to fit an additional object of size + * \additional. The growing (if any) will occur by doubling the existing + * allocation. + */ +static bool +grow_to_fit(struct blob *blob, size_t additional) +{ + size_t to_allocate; + uint8_t *new_data; + + if (blob->out_of_memory) + return false; + + if (blob->size + additional <= blob->allocated) + return true; + + if (blob->allocated == 0) + to_allocate = BLOB_INITIAL_SIZE; + else + to_allocate = blob->allocated * 2; + + to_allocate = MAX2(to_allocate, blob->allocated + additional); + + new_data = realloc(blob->data, to_allocate); + if (new_data == NULL) { + blob->out_of_memory = true; + return false; + } + + blob->data = new_data; + blob->allocated = to_allocate; + + return true; +} + +/* Align the blob->size so that reading or writing a value at (blob->data + + * blob->size) will result in an access aligned to a granularity of \alignment + * bytes. + * + * \return True unless allocation fails + */ +static bool +align_blob(struct blob *blob, size_t alignment) +{ + const size_t new_size = ALIGN(blob->size, alignment); + + if (blob->size < new_size) { + if (!grow_to_fit(blob, new_size - blob->size)) + return false; + + memset(blob->data + blob->size, 0, new_size - blob->size); + blob->size = new_size; + } + + return true; +} + +static void +align_blob_reader(struct blob_reader *blob, size_t alignment) +{ + blob->current = blob->data + ALIGN(blob->current - blob->data, alignment); +} + +struct blob * +blob_create() +{ + struct blob *blob = (struct blob *) malloc(sizeof(struct blob)); + if (blob == NULL) + return NULL; + + blob->data = NULL; + blob->allocated = 0; + blob->size = 0; + blob->out_of_memory = false; + + return blob; +} + +bool +blob_overwrite_bytes(struct blob *blob, + size_t offset, + const void *bytes, + size_t to_write) +{ + /* Detect an attempt to overwrite data out of bounds. */ + if (blob->size < offset + to_write) + return false; + + VG(VALGRIND_CHECK_MEM_IS_DEFINED(bytes, to_write)); + + memcpy(blob->data + offset, bytes, to_write); + + return true; +} + +bool +blob_write_bytes(struct blob *blob, const void *bytes, size_t to_write) +{ + if (! grow_to_fit(blob, to_write)) + return false; + + VG(VALGRIND_CHECK_MEM_IS_DEFINED(bytes, to_write)); + + memcpy(blob->data + blob->size, bytes, to_write); + blob->size += to_write; + + return true; +} + +uint8_t * +blob_reserve_bytes(struct blob *blob, size_t to_write) +{ + uint8_t *ret; + + if (! grow_to_fit (blob, to_write)) + return NULL; + + ret = blob->data + blob->size; + blob->size += to_write; + + return ret; +} + +bool +blob_write_uint32(struct blob *blob, uint32_t value) +{ + align_blob(blob, sizeof(value)); + + return blob_write_bytes(blob, &value, sizeof(value)); +} + +bool +blob_overwrite_uint32 (struct blob *blob, + size_t offset, + uint32_t value) +{ + return blob_overwrite_bytes(blob, offset, &value, sizeof(value)); +} + +bool +blob_write_uint64(struct blob *blob, uint64_t value) +{ + align_blob(blob, sizeof(value)); + + return blob_write_bytes(blob, &value, sizeof(value)); +} + +bool +blob_write_intptr(struct blob *blob, intptr_t value) +{ + align_blob(blob, sizeof(value)); + + return blob_write_bytes(blob, &value, sizeof(value)); +} + +bool +blob_write_string(struct blob *blob, const char *str) +{ + return blob_write_bytes(blob, str, strlen(str) + 1); +} + +void +blob_reader_init(struct blob_reader *blob, uint8_t *data, size_t size) +{ + blob->data = data; + blob->end = data + size; + blob->current = data; + blob->overrun = false; +} + +/* Check that an object of size \size can be read from this blob. + * + * If not, set blob->overrun to indicate that we attempted to read too far. + */ +static bool +ensure_can_read(struct blob_reader *blob, size_t size) +{ + if (blob->overrun) + return false; + + if (blob->current < blob->end && blob->end - blob->current >= size) + return true; + + blob->overrun = true; + + return false; +} + +void * +blob_read_bytes(struct blob_reader *blob, size_t size) +{ + void *ret; + + if (! ensure_can_read (blob, size)) + return NULL; + + ret = blob->current; + + blob->current += size; + + return ret; +} + +void +blob_copy_bytes(struct blob_reader *blob, uint8_t *dest, size_t size) +{ + uint8_t *bytes; + + bytes = blob_read_bytes(blob, size); + if (bytes == NULL) + return; + + memcpy(dest, bytes, size); +} + +/* These next three read functions have identical form. If we add any beyond + * these first three we should probably switch to generating these with a + * preprocessor macro. +*/ +uint32_t +blob_read_uint32(struct blob_reader *blob) +{ + uint32_t ret; + int size = sizeof(ret); + + align_blob_reader(blob, size); + + if (! ensure_can_read(blob, size)) + return 0; + + ret = *((uint32_t*) blob->current); + + blob->current += size; + + return ret; +} + +uint64_t +blob_read_uint64(struct blob_reader *blob) +{ + uint64_t ret; + int size = sizeof(ret); + + align_blob_reader(blob, size); + + if (! ensure_can_read(blob, size)) + return 0; + + ret = *((uint64_t*) blob->current); + + blob->current += size; + + return ret; +} + +intptr_t +blob_read_intptr(struct blob_reader *blob) +{ + intptr_t ret; + int size = sizeof(ret); + + align_blob_reader(blob, size); + + if (! ensure_can_read(blob, size)) + return 0; + + ret = *((intptr_t *) blob->current); + + blob->current += size; + + return ret; +} + +char * +blob_read_string(struct blob_reader *blob) +{ + int size; + char *ret; + uint8_t *nul; + + /* If we're already at the end, then this is an overrun. */ + if (blob->current >= blob->end) { + blob->overrun = true; + return NULL; + } + + /* Similarly, if there is no zero byte in the data remaining in this blob, + * we also consider that an overrun. + */ + nul = memchr(blob->current, 0, blob->end - blob->current); + + if (nul == NULL) { + blob->overrun = true; + return NULL; + } + + size = nul - blob->current + 1; + + assert(ensure_can_read(blob, size)); + + ret = (char *) blob->current; + + blob->current += size; + + return ret; +} diff --git a/src/compiler/blob.h b/src/compiler/blob.h new file mode 100644 index 00000000000..4cbbb01b15d --- /dev/null +++ b/src/compiler/blob.h @@ -0,0 +1,307 @@ +/* + * Copyright © 2014 Intel Corporation + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +#ifndef BLOB_H +#define BLOB_H + +#include +#include +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/* The blob functions implement a simple, low-level API for serializing and + * deserializing. + * + * All objects written to a blob will be serialized directly, (without any + * additional meta-data to describe the data written). Therefore, it is the + * caller's responsibility to ensure that any data can be read later, (either + * by knowing exactly what data is expected, or by writing to the blob + * sufficient meta-data to describe what has been written). + * + * A blob is efficient in that it dynamically grows by doubling in size, so + * allocation costs are logarithmic. + */ + +struct blob { + /* The data actually written to the blob. */ + uint8_t *data; + + /** Number of bytes that have been allocated for \c data. */ + size_t allocated; + + /** The number of bytes that have actual data written to them. */ + size_t size; + + /** + * True if we've ever failed to realloc or if we go pas the end of a fixed + * allocation blob. + */ + bool out_of_memory; +}; + +/* When done reading, the caller can ensure that everything was consumed by + * checking the following: + * + * 1. blob->current should be equal to blob->end, (if not, too little was + * read). + * + * 2. blob->overrun should be false, (otherwise, too much was read). + */ +struct blob_reader { + uint8_t *data; + uint8_t *end; + uint8_t *current; + bool overrun; +}; + +/** + * Create a new, empty blob. + * + * \return The new blob, (or NULL in case of allocation failure). + */ +struct blob * +blob_create(void); + +/** + * Destroy a blob and free its memory. + */ +static inline void +blob_destroy(struct blob *blob) +{ + free(blob->data); + free(blob); +} + +/** + * Add some unstructured, fixed-size data to a blob. + * + * \return True unless allocation failed. + */ +bool +blob_write_bytes(struct blob *blob, const void *bytes, size_t to_write); + +/** + * Reserve space in \blob for a number of bytes. + * + * Space will be allocated within the blob for these byes, but the bytes will + * be left uninitialized. The caller is expected to use the return value to + * write directly (and immediately) to these bytes. + * + * \note The return value is valid immediately upon return, but can be + * invalidated by any other call to a blob function. So the caller should call + * blob_reserve_byes immediately before writing through the returned pointer. + * + * This function is intended to be used when interfacing with an existing API + * that is not aware of the blob API, (so that blob_write_bytes cannot be + * called). + * + * \return A pointer to space allocated within \blob to which \to_write bytes + * can be written, (or NULL in case of any allocation error). + */ +uint8_t * +blob_reserve_bytes(struct blob *blob, size_t to_write); + +/** + * Overwrite some data previously written to the blob. + * + * Writes data to an existing portion of the blob at an offset of \offset. + * This data range must have previously been written to the blob by one of the + * blob_write_* calls. + * + * For example usage, see blob_overwrite_uint32 + * + * \return True unless the requested offset or offset+to_write lie outside + * the current blob's size. + */ +bool +blob_overwrite_bytes(struct blob *blob, + size_t offset, + const void *bytes, + size_t to_write); + +/** + * Add a uint32_t to a blob. + * + * \note This function will only write to a uint32_t-aligned offset from the + * beginning of the blob's data, so some padding bytes may be added to the + * blob if this write follows some unaligned write (such as + * blob_write_string). + * + * \return True unless allocation failed. + */ +bool +blob_write_uint32(struct blob *blob, uint32_t value); + +/** + * Overwrite a uint32_t previously written to the blob. + * + * Writes a uint32_t value to an existing portion of the blob at an offset of + * \offset. This data range must have previously been written to the blob by + * one of the blob_write_* calls. + * + * + * The expected usage is something like the following pattern: + * + * size_t offset; + * + * offset = blob->size; + * blob_write_uint32 (blob, 0); // placeholder + * ... various blob write calls, writing N items ... + * blob_overwrite_uint32 (blob, offset, N); + * + * \return True unless the requested position or position+to_write lie outside + * the current blob's size. + */ +bool +blob_overwrite_uint32(struct blob *blob, + size_t offset, + uint32_t value); + +/** + * Add a uint64_t to a blob. + * + * \note This function will only write to a uint64_t-aligned offset from the + * beginning of the blob's data, so some padding bytes may be added to the + * blob if this write follows some unaligned write (such as + * blob_write_string). + * + * \return True unless allocation failed. + */ +bool +blob_write_uint64(struct blob *blob, uint64_t value); + +/** + * Add an intptr_t to a blob. + * + * \note This function will only write to an intptr_t-aligned offset from the + * beginning of the blob's data, so some padding bytes may be added to the + * blob if this write follows some unaligned write (such as + * blob_write_string). + * + * \return True unless allocation failed. + */ +bool +blob_write_intptr(struct blob *blob, intptr_t value); + +/** + * Add a NULL-terminated string to a blob, (including the NULL terminator). + * + * \return True unless allocation failed. + */ +bool +blob_write_string(struct blob *blob, const char *str); + +/** + * Start reading a blob, (initializing the contents of \blob for reading). + * + * After this call, the caller can use the various blob_read_* functions to + * read elements from the data array. + * + * For all of the blob_read_* functions, if there is insufficient data + * remaining, the functions will do nothing, (perhaps returning default values + * such as 0). The caller can detect this by noting that the blob_reader's + * current value is unchanged before and after the call. + */ +void +blob_reader_init(struct blob_reader *blob, uint8_t *data, size_t size); + +/** + * Read some unstructured, fixed-size data from the current location, (and + * update the current location to just past this data). + * + * \note The memory returned belongs to the data underlying the blob reader. The + * caller must copy the data in order to use it after the lifetime of the data + * underlying the blob reader. + * + * \return The bytes read (see note above about memory lifetime). + */ +void * +blob_read_bytes(struct blob_reader *blob, size_t size); + +/** + * Read some unstructured, fixed-size data from the current location, copying + * it to \dest (and update the current location to just past this data) + */ +void +blob_copy_bytes(struct blob_reader *blob, uint8_t *dest, size_t size); + +/** + * Read a uint32_t from the current location, (and update the current location + * to just past this uint32_t). + * + * \note This function will only read from a uint32_t-aligned offset from the + * beginning of the blob's data, so some padding bytes may be skipped. + * + * \return The uint32_t read + */ +uint32_t +blob_read_uint32(struct blob_reader *blob); + +/** + * Read a uint64_t from the current location, (and update the current location + * to just past this uint64_t). + * + * \note This function will only read from a uint64_t-aligned offset from the + * beginning of the blob's data, so some padding bytes may be skipped. + * + * \return The uint64_t read + */ +uint64_t +blob_read_uint64(struct blob_reader *blob); + +/** + * Read an intptr_t value from the current location, (and update the + * current location to just past this intptr_t). + * + * \note This function will only read from an intptr_t-aligned offset from the + * beginning of the blob's data, so some padding bytes may be skipped. + * + * \return The intptr_t read + */ +intptr_t +blob_read_intptr(struct blob_reader *blob); + +/** + * Read a NULL-terminated string from the current location, (and update the + * current location to just past this string). + * + * \note The memory returned belongs to the data underlying the blob reader. The + * caller must copy the string in order to use the string after the lifetime + * of the data underlying the blob reader. + * + * \return The string read (see note above about memory lifetime). However, if + * there is no NULL byte remaining within the blob, this function returns + * NULL. + */ +char * +blob_read_string(struct blob_reader *blob); + +#ifdef __cplusplus +} +#endif + +#endif /* BLOB_H */ diff --git a/src/compiler/glsl/blob.c b/src/compiler/glsl/blob.c deleted file mode 100644 index 65e1376e160..00000000000 --- a/src/compiler/glsl/blob.c +++ /dev/null @@ -1,344 +0,0 @@ -/* - * Copyright © 2014 Intel Corporation - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice (including the next - * paragraph) shall be included in all copies or substantial portions of the - * Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS - * IN THE SOFTWARE. - */ - -#include - -#include "main/macros.h" -#include "blob.h" - -#ifdef HAVE_VALGRIND -#include -#include -#define VG(x) x -#else -#define VG(x) -#endif - -#define BLOB_INITIAL_SIZE 4096 - -/* Ensure that \blob will be able to fit an additional object of size - * \additional. The growing (if any) will occur by doubling the existing - * allocation. - */ -static bool -grow_to_fit(struct blob *blob, size_t additional) -{ - size_t to_allocate; - uint8_t *new_data; - - if (blob->out_of_memory) - return false; - - if (blob->size + additional <= blob->allocated) - return true; - - if (blob->allocated == 0) - to_allocate = BLOB_INITIAL_SIZE; - else - to_allocate = blob->allocated * 2; - - to_allocate = MAX2(to_allocate, blob->allocated + additional); - - new_data = realloc(blob->data, to_allocate); - if (new_data == NULL) { - blob->out_of_memory = true; - return false; - } - - blob->data = new_data; - blob->allocated = to_allocate; - - return true; -} - -/* Align the blob->size so that reading or writing a value at (blob->data + - * blob->size) will result in an access aligned to a granularity of \alignment - * bytes. - * - * \return True unless allocation fails - */ -static bool -align_blob(struct blob *blob, size_t alignment) -{ - const size_t new_size = ALIGN(blob->size, alignment); - - if (blob->size < new_size) { - if (!grow_to_fit(blob, new_size - blob->size)) - return false; - - memset(blob->data + blob->size, 0, new_size - blob->size); - blob->size = new_size; - } - - return true; -} - -static void -align_blob_reader(struct blob_reader *blob, size_t alignment) -{ - blob->current = blob->data + ALIGN(blob->current - blob->data, alignment); -} - -struct blob * -blob_create() -{ - struct blob *blob = (struct blob *) malloc(sizeof(struct blob)); - if (blob == NULL) - return NULL; - - blob->data = NULL; - blob->allocated = 0; - blob->size = 0; - blob->out_of_memory = false; - - return blob; -} - -bool -blob_overwrite_bytes(struct blob *blob, - size_t offset, - const void *bytes, - size_t to_write) -{ - /* Detect an attempt to overwrite data out of bounds. */ - if (blob->size < offset + to_write) - return false; - - VG(VALGRIND_CHECK_MEM_IS_DEFINED(bytes, to_write)); - - memcpy(blob->data + offset, bytes, to_write); - - return true; -} - -bool -blob_write_bytes(struct blob *blob, const void *bytes, size_t to_write) -{ - if (! grow_to_fit(blob, to_write)) - return false; - - VG(VALGRIND_CHECK_MEM_IS_DEFINED(bytes, to_write)); - - memcpy(blob->data + blob->size, bytes, to_write); - blob->size += to_write; - - return true; -} - -uint8_t * -blob_reserve_bytes(struct blob *blob, size_t to_write) -{ - uint8_t *ret; - - if (! grow_to_fit (blob, to_write)) - return NULL; - - ret = blob->data + blob->size; - blob->size += to_write; - - return ret; -} - -bool -blob_write_uint32(struct blob *blob, uint32_t value) -{ - align_blob(blob, sizeof(value)); - - return blob_write_bytes(blob, &value, sizeof(value)); -} - -bool -blob_overwrite_uint32 (struct blob *blob, - size_t offset, - uint32_t value) -{ - return blob_overwrite_bytes(blob, offset, &value, sizeof(value)); -} - -bool -blob_write_uint64(struct blob *blob, uint64_t value) -{ - align_blob(blob, sizeof(value)); - - return blob_write_bytes(blob, &value, sizeof(value)); -} - -bool -blob_write_intptr(struct blob *blob, intptr_t value) -{ - align_blob(blob, sizeof(value)); - - return blob_write_bytes(blob, &value, sizeof(value)); -} - -bool -blob_write_string(struct blob *blob, const char *str) -{ - return blob_write_bytes(blob, str, strlen(str) + 1); -} - -void -blob_reader_init(struct blob_reader *blob, uint8_t *data, size_t size) -{ - blob->data = data; - blob->end = data + size; - blob->current = data; - blob->overrun = false; -} - -/* Check that an object of size \size can be read from this blob. - * - * If not, set blob->overrun to indicate that we attempted to read too far. - */ -static bool -ensure_can_read(struct blob_reader *blob, size_t size) -{ - if (blob->overrun) - return false; - - if (blob->current < blob->end && blob->end - blob->current >= size) - return true; - - blob->overrun = true; - - return false; -} - -void * -blob_read_bytes(struct blob_reader *blob, size_t size) -{ - void *ret; - - if (! ensure_can_read (blob, size)) - return NULL; - - ret = blob->current; - - blob->current += size; - - return ret; -} - -void -blob_copy_bytes(struct blob_reader *blob, uint8_t *dest, size_t size) -{ - uint8_t *bytes; - - bytes = blob_read_bytes(blob, size); - if (bytes == NULL) - return; - - memcpy(dest, bytes, size); -} - -/* These next three read functions have identical form. If we add any beyond - * these first three we should probably switch to generating these with a - * preprocessor macro. -*/ -uint32_t -blob_read_uint32(struct blob_reader *blob) -{ - uint32_t ret; - int size = sizeof(ret); - - align_blob_reader(blob, size); - - if (! ensure_can_read(blob, size)) - return 0; - - ret = *((uint32_t*) blob->current); - - blob->current += size; - - return ret; -} - -uint64_t -blob_read_uint64(struct blob_reader *blob) -{ - uint64_t ret; - int size = sizeof(ret); - - align_blob_reader(blob, size); - - if (! ensure_can_read(blob, size)) - return 0; - - ret = *((uint64_t*) blob->current); - - blob->current += size; - - return ret; -} - -intptr_t -blob_read_intptr(struct blob_reader *blob) -{ - intptr_t ret; - int size = sizeof(ret); - - align_blob_reader(blob, size); - - if (! ensure_can_read(blob, size)) - return 0; - - ret = *((intptr_t *) blob->current); - - blob->current += size; - - return ret; -} - -char * -blob_read_string(struct blob_reader *blob) -{ - int size; - char *ret; - uint8_t *nul; - - /* If we're already at the end, then this is an overrun. */ - if (blob->current >= blob->end) { - blob->overrun = true; - return NULL; - } - - /* Similarly, if there is no zero byte in the data remaining in this blob, - * we also consider that an overrun. - */ - nul = memchr(blob->current, 0, blob->end - blob->current); - - if (nul == NULL) { - blob->overrun = true; - return NULL; - } - - size = nul - blob->current + 1; - - assert(ensure_can_read(blob, size)); - - ret = (char *) blob->current; - - blob->current += size; - - return ret; -} diff --git a/src/compiler/glsl/blob.h b/src/compiler/glsl/blob.h deleted file mode 100644 index 4cbbb01b15d..00000000000 --- a/src/compiler/glsl/blob.h +++ /dev/null @@ -1,307 +0,0 @@ -/* - * Copyright © 2014 Intel Corporation - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice (including the next - * paragraph) shall be included in all copies or substantial portions of the - * Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS - * IN THE SOFTWARE. - */ - -#ifndef BLOB_H -#define BLOB_H - -#include -#include -#include -#include - -#ifdef __cplusplus -extern "C" { -#endif - -/* The blob functions implement a simple, low-level API for serializing and - * deserializing. - * - * All objects written to a blob will be serialized directly, (without any - * additional meta-data to describe the data written). Therefore, it is the - * caller's responsibility to ensure that any data can be read later, (either - * by knowing exactly what data is expected, or by writing to the blob - * sufficient meta-data to describe what has been written). - * - * A blob is efficient in that it dynamically grows by doubling in size, so - * allocation costs are logarithmic. - */ - -struct blob { - /* The data actually written to the blob. */ - uint8_t *data; - - /** Number of bytes that have been allocated for \c data. */ - size_t allocated; - - /** The number of bytes that have actual data written to them. */ - size_t size; - - /** - * True if we've ever failed to realloc or if we go pas the end of a fixed - * allocation blob. - */ - bool out_of_memory; -}; - -/* When done reading, the caller can ensure that everything was consumed by - * checking the following: - * - * 1. blob->current should be equal to blob->end, (if not, too little was - * read). - * - * 2. blob->overrun should be false, (otherwise, too much was read). - */ -struct blob_reader { - uint8_t *data; - uint8_t *end; - uint8_t *current; - bool overrun; -}; - -/** - * Create a new, empty blob. - * - * \return The new blob, (or NULL in case of allocation failure). - */ -struct blob * -blob_create(void); - -/** - * Destroy a blob and free its memory. - */ -static inline void -blob_destroy(struct blob *blob) -{ - free(blob->data); - free(blob); -} - -/** - * Add some unstructured, fixed-size data to a blob. - * - * \return True unless allocation failed. - */ -bool -blob_write_bytes(struct blob *blob, const void *bytes, size_t to_write); - -/** - * Reserve space in \blob for a number of bytes. - * - * Space will be allocated within the blob for these byes, but the bytes will - * be left uninitialized. The caller is expected to use the return value to - * write directly (and immediately) to these bytes. - * - * \note The return value is valid immediately upon return, but can be - * invalidated by any other call to a blob function. So the caller should call - * blob_reserve_byes immediately before writing through the returned pointer. - * - * This function is intended to be used when interfacing with an existing API - * that is not aware of the blob API, (so that blob_write_bytes cannot be - * called). - * - * \return A pointer to space allocated within \blob to which \to_write bytes - * can be written, (or NULL in case of any allocation error). - */ -uint8_t * -blob_reserve_bytes(struct blob *blob, size_t to_write); - -/** - * Overwrite some data previously written to the blob. - * - * Writes data to an existing portion of the blob at an offset of \offset. - * This data range must have previously been written to the blob by one of the - * blob_write_* calls. - * - * For example usage, see blob_overwrite_uint32 - * - * \return True unless the requested offset or offset+to_write lie outside - * the current blob's size. - */ -bool -blob_overwrite_bytes(struct blob *blob, - size_t offset, - const void *bytes, - size_t to_write); - -/** - * Add a uint32_t to a blob. - * - * \note This function will only write to a uint32_t-aligned offset from the - * beginning of the blob's data, so some padding bytes may be added to the - * blob if this write follows some unaligned write (such as - * blob_write_string). - * - * \return True unless allocation failed. - */ -bool -blob_write_uint32(struct blob *blob, uint32_t value); - -/** - * Overwrite a uint32_t previously written to the blob. - * - * Writes a uint32_t value to an existing portion of the blob at an offset of - * \offset. This data range must have previously been written to the blob by - * one of the blob_write_* calls. - * - * - * The expected usage is something like the following pattern: - * - * size_t offset; - * - * offset = blob->size; - * blob_write_uint32 (blob, 0); // placeholder - * ... various blob write calls, writing N items ... - * blob_overwrite_uint32 (blob, offset, N); - * - * \return True unless the requested position or position+to_write lie outside - * the current blob's size. - */ -bool -blob_overwrite_uint32(struct blob *blob, - size_t offset, - uint32_t value); - -/** - * Add a uint64_t to a blob. - * - * \note This function will only write to a uint64_t-aligned offset from the - * beginning of the blob's data, so some padding bytes may be added to the - * blob if this write follows some unaligned write (such as - * blob_write_string). - * - * \return True unless allocation failed. - */ -bool -blob_write_uint64(struct blob *blob, uint64_t value); - -/** - * Add an intptr_t to a blob. - * - * \note This function will only write to an intptr_t-aligned offset from the - * beginning of the blob's data, so some padding bytes may be added to the - * blob if this write follows some unaligned write (such as - * blob_write_string). - * - * \return True unless allocation failed. - */ -bool -blob_write_intptr(struct blob *blob, intptr_t value); - -/** - * Add a NULL-terminated string to a blob, (including the NULL terminator). - * - * \return True unless allocation failed. - */ -bool -blob_write_string(struct blob *blob, const char *str); - -/** - * Start reading a blob, (initializing the contents of \blob for reading). - * - * After this call, the caller can use the various blob_read_* functions to - * read elements from the data array. - * - * For all of the blob_read_* functions, if there is insufficient data - * remaining, the functions will do nothing, (perhaps returning default values - * such as 0). The caller can detect this by noting that the blob_reader's - * current value is unchanged before and after the call. - */ -void -blob_reader_init(struct blob_reader *blob, uint8_t *data, size_t size); - -/** - * Read some unstructured, fixed-size data from the current location, (and - * update the current location to just past this data). - * - * \note The memory returned belongs to the data underlying the blob reader. The - * caller must copy the data in order to use it after the lifetime of the data - * underlying the blob reader. - * - * \return The bytes read (see note above about memory lifetime). - */ -void * -blob_read_bytes(struct blob_reader *blob, size_t size); - -/** - * Read some unstructured, fixed-size data from the current location, copying - * it to \dest (and update the current location to just past this data) - */ -void -blob_copy_bytes(struct blob_reader *blob, uint8_t *dest, size_t size); - -/** - * Read a uint32_t from the current location, (and update the current location - * to just past this uint32_t). - * - * \note This function will only read from a uint32_t-aligned offset from the - * beginning of the blob's data, so some padding bytes may be skipped. - * - * \return The uint32_t read - */ -uint32_t -blob_read_uint32(struct blob_reader *blob); - -/** - * Read a uint64_t from the current location, (and update the current location - * to just past this uint64_t). - * - * \note This function will only read from a uint64_t-aligned offset from the - * beginning of the blob's data, so some padding bytes may be skipped. - * - * \return The uint64_t read - */ -uint64_t -blob_read_uint64(struct blob_reader *blob); - -/** - * Read an intptr_t value from the current location, (and update the - * current location to just past this intptr_t). - * - * \note This function will only read from an intptr_t-aligned offset from the - * beginning of the blob's data, so some padding bytes may be skipped. - * - * \return The intptr_t read - */ -intptr_t -blob_read_intptr(struct blob_reader *blob); - -/** - * Read a NULL-terminated string from the current location, (and update the - * current location to just past this string). - * - * \note The memory returned belongs to the data underlying the blob reader. The - * caller must copy the string in order to use the string after the lifetime - * of the data underlying the blob reader. - * - * \return The string read (see note above about memory lifetime). However, if - * there is no NULL byte remaining within the blob, this function returns - * NULL. - */ -char * -blob_read_string(struct blob_reader *blob); - -#ifdef __cplusplus -} -#endif - -#endif /* BLOB_H */ diff --git a/src/compiler/glsl/meson.build b/src/compiler/glsl/meson.build index 1d7e40b55be..d1a75eb8c36 100644 --- a/src/compiler/glsl/meson.build +++ b/src/compiler/glsl/meson.build @@ -58,8 +58,6 @@ files_libglsl = files( 'ast_function.cpp', 'ast_to_hir.cpp', 'ast_type.cpp', - 'blob.c', - 'blob.h', 'builtin_functions.cpp', 'builtin_functions.h', 'builtin_int64.h', @@ -205,7 +203,6 @@ libglsl = static_library( cpp_args : [cpp_vis_args, cpp_msvc_compat_args], link_with : [libnir, libglcpp], include_directories : [inc_common, inc_compiler, inc_nir], - dependencies : [dep_valgrind], build_by_default : false, ) diff --git a/src/compiler/meson.build b/src/compiler/meson.build index 58d52e4c6cb..783be11c926 100644 --- a/src/compiler/meson.build +++ b/src/compiler/meson.build @@ -23,6 +23,8 @@ inc_nir = include_directories('nir') inc_glsl = include_directories('glsl') files_libcompiler = files( + 'blob.c', + 'blob.h', 'builtin_type_macros.h', 'glsl_types.cpp', 'glsl_types.h', @@ -47,6 +49,7 @@ libcompiler = static_library( include_directories : [inc_mapi, inc_mesa, inc_compiler, inc_common], c_args : [c_vis_args, c_msvc_compat_args, no_override_init_args], cpp_args : [cpp_vis_args, cpp_msvc_compat_args], + dependencies : [dep_valgrind], build_by_default : false, ) diff --git a/src/mesa/state_tracker/st_shader_cache.h b/src/mesa/state_tracker/st_shader_cache.h index f9e46158dc0..090d7d85cc8 100644 --- a/src/mesa/state_tracker/st_shader_cache.h +++ b/src/mesa/state_tracker/st_shader_cache.h @@ -22,7 +22,7 @@ */ #include "st_context.h" -#include "compiler/glsl/blob.h" +#include "compiler/blob.h" #include "main/mtypes.h" #include "pipe/p_state.h" #include "util/disk_cache.h"