Esempio n. 1
0
static nir_variable *
read_variable(read_ctx *ctx)
{
   nir_variable *var = rzalloc(ctx->nir, nir_variable);
   read_add_object(ctx, var);

   var->type = decode_type_from_blob(ctx->blob);
   bool has_name = blob_read_uint32(ctx->blob);
   if (has_name) {
      const char *name = blob_read_string(ctx->blob);
      var->name = ralloc_strdup(var, name);
   } else {
      var->name = NULL;
   }
   blob_copy_bytes(ctx->blob, (uint8_t *) &var->data, sizeof(var->data));
   var->num_state_slots = blob_read_uint32(ctx->blob);
   var->state_slots = ralloc_array(var, nir_state_slot, var->num_state_slots);
   blob_copy_bytes(ctx->blob, (uint8_t *) var->state_slots,
                   var->num_state_slots * sizeof(nir_state_slot));
   bool has_const_initializer = blob_read_uint32(ctx->blob);
   if (has_const_initializer)
      var->constant_initializer = read_constant(ctx, var);
   else
      var->constant_initializer = NULL;
   bool has_interface_type = blob_read_uint32(ctx->blob);
   if (has_interface_type)
      var->interface_type = decode_type_from_blob(ctx->blob);
   else
      var->interface_type = NULL;
   var->num_members = blob_read_uint32(ctx->blob);
   if (var->num_members > 0) {
      var->members = ralloc_array(var, struct nir_variable_data,
                                  var->num_members);
      blob_copy_bytes(ctx->blob, (uint8_t *) var->members,
                      var->num_members * sizeof(*var->members));
   }
Esempio n. 2
0
/* Test at least one call of each blob_write_foo and blob_read_foo function,
 * verifying that we read out everything we wrote, that every bytes is
 * consumed, and that the overrun bit is not set.
 */
static void
test_write_and_read_functions (void)
{
   struct blob blob;
   struct blob_reader reader;
   ssize_t reserved;
   size_t str_offset, uint_offset;
   uint8_t reserve_buf[sizeof(reserve_test_str)];

   blob_init(&blob);

   /*** Test blob by writing one of every possible kind of value. */

   blob_write_bytes(&blob, bytes_test_str, sizeof(bytes_test_str));

   reserved = blob_reserve_bytes(&blob, sizeof(reserve_test_str));
   blob_overwrite_bytes(&blob, reserved, reserve_test_str, sizeof(reserve_test_str));

   /* Write a placeholder, (to be replaced later via overwrite_bytes) */
   str_offset = blob.size;
   blob_write_bytes(&blob, placeholder_str, sizeof(placeholder_str));

   blob_write_uint32(&blob, uint32_test);

   /* Write a placeholder, (to be replaced later via overwrite_uint32) */
   uint_offset = blob.size;
   blob_write_uint32(&blob, uint32_placeholder);

   blob_write_uint64(&blob, uint64_test);

   blob_write_intptr(&blob, (intptr_t) &blob);

   blob_write_string(&blob, string_test_str);

   /* Finally, overwrite our placeholders. */
   blob_overwrite_bytes(&blob, str_offset, overwrite_test_str,
                        sizeof(overwrite_test_str));
   blob_overwrite_uint32(&blob, uint_offset, uint32_overwrite);

   /*** Now read each value and verify. */
   blob_reader_init(&reader, blob.data, blob.size);

   expect_equal_str(bytes_test_str,
                    blob_read_bytes(&reader, sizeof(bytes_test_str)),
                    "blob_write/read_bytes");

   blob_copy_bytes(&reader, reserve_buf, sizeof(reserve_buf));
   expect_equal_str(reserve_test_str, (char *) reserve_buf,
                    "blob_reserve_bytes/blob_copy_bytes");

   expect_equal_str(overwrite_test_str,
                    blob_read_bytes(&reader, sizeof(overwrite_test_str)),
                    "blob_overwrite_bytes");

   expect_equal(uint32_test, blob_read_uint32(&reader),
                "blob_write/read_uint32");
   expect_equal(uint32_overwrite, blob_read_uint32(&reader),
                "blob_overwrite_uint32");
   expect_equal(uint64_test, blob_read_uint64(&reader),
                "blob_write/read_uint64");
   expect_equal((intptr_t) &blob, blob_read_intptr(&reader),
                "blob_write/read_intptr");
   expect_equal_str(string_test_str, blob_read_string(&reader),
                    "blob_write/read_string");

   expect_equal(reader.end - reader.data, reader.current - reader.data,
                "read_consumes_all_bytes");
   expect_equal(false, reader.overrun, "read_does_not_overrun");

   blob_finish(&blob);
}