Task Description: Implement a Huge Page Memory Allocator
You are required to implement a JemallocHugePageAllocator class in C++ that provides memory allocation functionalities using huge pages. Your implementation should adhere to the following specifications to ensure that all provided tests pass:
Initialization:
Create a static method bool init(int numHugePages) that initializes the allocator with a specified number of huge pages.
If initialized successfully, the method should return true and reserve the necessary memory for allocations.
Maintain a static variable to track whether the allocator is initialized.
Memory Allocation:
Implement a static method void* allocate(size_t size) that allocates memory of the given size.
If the allocator is not initialized, allocate memory using default allocation methods (e.g., malloc).
If initialized, try to allocate memory from the huge page arena. If allocation cannot be satisfied from the huge pages, fallback to standard memory allocation.
Memory Deallocation:
Implement a static method void deallocate(void* ptr) that handles memory deallocation.
If the memory was allocated from the huge page arena, it should release the memory back to the huge page allocator; otherwise, use standard deallocation methods (e.g., free).
Memory Status:
Implement a static method size_t freeSpace() that returns the amount of free memory available within the huge page arena after initialization.
Address Check:
Implement a static method bool addressInArena(void* ptr) that checks if a given memory address is within the allocated huge page arena.
Memory Layout:
Ensure that large allocations (e.g., 2MB pages) are reserved correctly and that smaller allocations fit appropriately within those pages.
The allocator should properly handle multiple allocations and deallocations without memory leaks.
Concurrency:
If the implementation is to be extended, consider that the allocator may need to manage memory in a multi-threaded context, requiring appropriate synchronization mechanisms (not specified in the current tests but may be relevant for future functionalities).
Your implementation must be contained in the /app/ directory in a file named JemallocHugePageAllocator.cpp. Include necessary header files and ensure that the code compiles correctly with the provided test cases.