Skip to content

Commit

Permalink
Add TypeFactory
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 586456818
  • Loading branch information
jcking authored and copybara-github committed Nov 29, 2023
1 parent b4bee74 commit 749c600
Show file tree
Hide file tree
Showing 15 changed files with 1,060 additions and 1,294 deletions.
5 changes: 4 additions & 1 deletion common/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ cc_library(
"types/*_test.cc",
"values/*_test.cc",
],
),
) + ["type_factory.cc"],
hdrs = glob(
[
"types/*.h",
Expand All @@ -257,6 +257,7 @@ cc_library(
],
) + [
"type.h",
"type_factory.h",
"type_interface.h",
"value.h",
"value_interface.h",
Expand Down Expand Up @@ -289,6 +290,7 @@ cc_library(
"@com_google_absl//absl/strings",
"@com_google_absl//absl/strings:cord",
"@com_google_absl//absl/strings:string_view",
"@com_google_absl//absl/synchronization",
"@com_google_absl//absl/time",
"@com_google_absl//absl/types:optional",
"@com_google_absl//absl/types:span",
Expand All @@ -303,6 +305,7 @@ cc_test(
"types/*_test.cc",
"values/*_test.cc",
]) + [
"type_factory_test.cc",
"type_test.cc",
"value_test.cc",
],
Expand Down
39 changes: 39 additions & 0 deletions common/memory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,40 @@ class UnreachablePoolingMemoryManager final : public PoolingMemoryManager {
}
};

struct LeakyPoolingMemoryManager {
LeakyPoolingMemoryManager() = default;
LeakyPoolingMemoryManager(const LeakyPoolingMemoryManager&) = delete;
LeakyPoolingMemoryManager(LeakyPoolingMemoryManager&&) = delete;
LeakyPoolingMemoryManager& operator=(const LeakyPoolingMemoryManager&) =
delete;
LeakyPoolingMemoryManager& operator=(LeakyPoolingMemoryManager&&) = delete;
};

absl::Nonnull<void*> LeakyPoolingMemoryManagerAllocate(void*, size_t size,
size_t align) {
if (align <= __STDCPP_DEFAULT_NEW_ALIGNMENT__) {
return ::operator new(size);
}
return ::operator new(size, static_cast<std::align_val_t>(align));
}

bool LeakyPoolingMemoryManagerDeallocate(absl::Nonnull<void*>,
absl::Nonnull<void*>, size_t,
size_t) noexcept {
return false;
}

void LeakyPoolingMemoryManagerOwnCustomDestructor(
absl::Nonnull<void*>, absl::Nonnull<void*>,
absl::Nonnull<PoolingMemoryManagerVirtualTable::CustomDestructPtr>) {}

const PoolingMemoryManagerVirtualTable kLeakyMemoryManagerVirtualTable = {
NativeTypeId::For<LeakyPoolingMemoryManager>(),
&LeakyPoolingMemoryManagerAllocate,
&LeakyPoolingMemoryManagerDeallocate,
&LeakyPoolingMemoryManagerOwnCustomDestructor,
};

} // namespace

std::ostream& operator<<(std::ostream& out,
Expand All @@ -338,4 +372,9 @@ MemoryManager::UnreachablePooling() noexcept {
return &instance.get();
}

MemoryManagerRef MemoryManagerRef::Leaky() {
static LeakyPoolingMemoryManager instance;
return MemoryManagerRef::Pooling(kLeakyMemoryManagerVirtualTable, instance);
}

} // namespace cel
7 changes: 7 additions & 0 deletions common/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -872,6 +872,13 @@ class ABSL_ATTRIBUTE_TRIVIAL_ABI MemoryManagerRef final {
return memory_manager;
}

// Returns a `MemoryManagerRef` to a `PoolingMemoryManager` which never
// deallocates memory and is never destroyed.
//
// IMPORTANT: This should only be used for cases where something is
// initialized and used once. It should never be used for anything else.
static MemoryManagerRef Leaky();

MemoryManagerRef() = delete;
MemoryManagerRef(const MemoryManagerRef&) = default;
MemoryManagerRef(MemoryManagerRef&&) = default;
Expand Down
Loading

0 comments on commit 749c600

Please # to comment.