// Copyright (c) Meta Platforms, Inc. and affiliates.

#pragma once

#include <asmjit/asmjit.h>

#include <cstddef>

namespace jit {

// Resulting address and status code after calling ICodeAllocator::addCode().
struct AllocateResult {
  void* addr{nullptr};
  asmjit::Error error{asmjit::kErrorNotInitialized};
};

// Interface for allocating memory for compiled code generated by the JIT.
class ICodeAllocator {
 public:
  virtual ~ICodeAllocator() = default;

  // Add a compiled code object to the allocator.  Get its new address on
  // success, or nullptr and an error on failure.
  virtual AllocateResult addCode(asmjit::CodeHolder* code) = 0;

  // Deallocate a compiled code object.
  virtual asmjit::Error releaseCode(void* code) = 0;

  // Check if a pointer is located within this allocator's memory.
  virtual bool contains(const void* ptr) const = 0;

  // Get the number of bytes of memory used by the allocator.
  virtual size_t usedBytes() const = 0;

  // Get the asmjit environment used by this allocator.
  virtual const asmjit::Environment& asmJitEnvironment() const = 0;
};

} // namespace jit
