parent
c05b320d9c
commit
10c3e3dfb6
7 changed files with 132 additions and 99 deletions
@ -0,0 +1,63 @@ |
||||
// Protocol Buffers - Google's data interchange format
|
||||
// Copyright 2023 Google LLC. All rights reserved.
|
||||
//
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file or at
|
||||
// https://developers.google.com/open-source/licenses/bsd
|
||||
|
||||
#include "upb/message/internal/extension.h" |
||||
|
||||
#include <string.h> |
||||
|
||||
#include "upb/mem/arena.h" |
||||
#include "upb/message/internal/extension.h" |
||||
#include "upb/message/internal/message.h" |
||||
#include "upb/message/types.h" |
||||
#include "upb/mini_table/extension.h" |
||||
|
||||
// Must be last.
|
||||
#include "upb/port/def.inc" |
||||
|
||||
const upb_Message_Extension* _upb_Message_Getext( |
||||
const upb_Message* msg, const upb_MiniTableExtension* e) { |
||||
size_t n; |
||||
const upb_Message_Extension* ext = _upb_Message_Getexts(msg, &n); |
||||
|
||||
// For now we use linear search exclusively to find extensions.
|
||||
// If this becomes an issue due to messages with lots of extensions,
|
||||
// we can introduce a table of some sort.
|
||||
for (size_t i = 0; i < n; i++) { |
||||
if (ext[i].ext == e) { |
||||
return &ext[i]; |
||||
} |
||||
} |
||||
|
||||
return NULL; |
||||
} |
||||
|
||||
const upb_Message_Extension* _upb_Message_Getexts(const upb_Message* msg, |
||||
size_t* count) { |
||||
const upb_Message_Internal* in = upb_Message_Getinternal(msg); |
||||
if (in->internal) { |
||||
*count = (in->internal->size - in->internal->ext_begin) / |
||||
sizeof(upb_Message_Extension); |
||||
return UPB_PTR_AT(in->internal, in->internal->ext_begin, void); |
||||
} else { |
||||
*count = 0; |
||||
return NULL; |
||||
} |
||||
} |
||||
|
||||
upb_Message_Extension* _upb_Message_GetOrCreateExtension( |
||||
upb_Message* msg, const upb_MiniTableExtension* e, upb_Arena* arena) { |
||||
upb_Message_Extension* ext = |
||||
(upb_Message_Extension*)_upb_Message_Getext(msg, e); |
||||
if (ext) return ext; |
||||
if (!UPB_PRIVATE(_upb_Message_Realloc)(msg, sizeof(upb_Message_Extension), arena)) return NULL; |
||||
upb_Message_Internal* in = upb_Message_Getinternal(msg); |
||||
in->internal->ext_begin -= sizeof(upb_Message_Extension); |
||||
ext = UPB_PTR_AT(in->internal, in->internal->ext_begin, void); |
||||
memset(ext, 0, sizeof(upb_Message_Extension)); |
||||
ext->ext = e; |
||||
return ext; |
||||
} |
@ -0,0 +1,58 @@ |
||||
// Protocol Buffers - Google's data interchange format
|
||||
// Copyright 2023 Google LLC. All rights reserved.
|
||||
//
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file or at
|
||||
// https://developers.google.com/open-source/licenses/bsd
|
||||
|
||||
#include "upb/message/message.h" |
||||
|
||||
#include <math.h> |
||||
#include <string.h> |
||||
|
||||
#include "upb/base/internal/log2.h" |
||||
#include "upb/mem/arena.h" |
||||
#include "upb/message/internal/message.h" |
||||
#include "upb/message/internal/types.h" |
||||
|
||||
// Must be last.
|
||||
#include "upb/port/def.inc" |
||||
|
||||
const float kUpb_FltInfinity = INFINITY; |
||||
const double kUpb_Infinity = INFINITY; |
||||
const double kUpb_NaN = NAN; |
||||
|
||||
static const size_t realloc_overhead = sizeof(upb_Message_InternalData); |
||||
|
||||
bool UPB_PRIVATE(_upb_Message_Realloc)(upb_Message* msg, size_t need, |
||||
upb_Arena* arena) { |
||||
upb_Message_Internal* in = upb_Message_Getinternal(msg); |
||||
if (!in->internal) { |
||||
// No internal data, allocate from scratch.
|
||||
size_t size = UPB_MAX(128, upb_Log2CeilingSize(need + realloc_overhead)); |
||||
upb_Message_InternalData* internal = upb_Arena_Malloc(arena, size); |
||||
if (!internal) return false; |
||||
internal->size = size; |
||||
internal->unknown_end = realloc_overhead; |
||||
internal->ext_begin = size; |
||||
in->internal = internal; |
||||
} else if (in->internal->ext_begin - in->internal->unknown_end < need) { |
||||
// Internal data is too small, reallocate.
|
||||
size_t new_size = upb_Log2CeilingSize(in->internal->size + need); |
||||
size_t ext_bytes = in->internal->size - in->internal->ext_begin; |
||||
size_t new_ext_begin = new_size - ext_bytes; |
||||
upb_Message_InternalData* internal = |
||||
upb_Arena_Realloc(arena, in->internal, in->internal->size, new_size); |
||||
if (!internal) return false; |
||||
if (ext_bytes) { |
||||
// Need to move extension data to the end.
|
||||
char* ptr = (char*)internal; |
||||
memmove(ptr + new_ext_begin, ptr + internal->ext_begin, ext_bytes); |
||||
} |
||||
internal->ext_begin = new_ext_begin; |
||||
internal->size = new_size; |
||||
in->internal = internal; |
||||
} |
||||
UPB_ASSERT(in->internal->ext_begin - in->internal->unknown_end >= need); |
||||
return true; |
||||
} |
Loading…
Reference in new issue