diff --git a/hw1/Makefile b/hw1/Makefile new file mode 100644 index 0000000..f7fb31d --- /dev/null +++ b/hw1/Makefile @@ -0,0 +1,14 @@ +CC=g++ +STANDARD=-std=c++20 -g +TEST_PARAMS=-lgtest -lgmock -pthread + +build: + $(CC) $(STANDARD) allocator.cpp main.cpp -o main.out +run: + ./main.out + +test: + $(CC) $(STANDARD) allocator.cpp test.cpp -o test.out $(TEST_PARAMS) + +memory: + valgrind --leak-check=full --show-leak-kinds=all --track-origins=yes ./main.out diff --git a/hw1/allocator.cpp b/hw1/allocator.cpp new file mode 100644 index 0000000..ac4d6f3 --- /dev/null +++ b/hw1/allocator.cpp @@ -0,0 +1,29 @@ +#include "allocator.h" + +void Allocator::makeAllocator(size_t size) { + if (this->total_memory != nullptr) { + delete[] this->total_memory; + } + + this->size_of_allocator = size; + this->total_memory = new int8_t[size]; +} + +char* Allocator::alloc(size_t size) { + if (size > this->size_of_allocator - offset) { + return nullptr; + } + + int8_t* place = (this->total_memory + offset); + this->offset += size; + + return (char*)place; +} + +void Allocator::reset() { + this->offset == 0; +} + +Allocator::~Allocator() { + delete[] this->total_memory; +} diff --git a/hw1/allocator.h b/hw1/allocator.h new file mode 100644 index 0000000..d9235db --- /dev/null +++ b/hw1/allocator.h @@ -0,0 +1,22 @@ +#include +#include + +#ifndef ALLOCATOR_H +#define ALLOCATOR_H + +class Allocator +{ + public: + void makeAllocator(size_t maxSize); + char *alloc(size_t size); + void reset(); + + ~Allocator(); + + private: + int8_t* total_memory{nullptr}; + int8_t offset{0}; + int size_of_allocator{0}; +}; + +#endif diff --git a/hw1/main.cpp b/hw1/main.cpp new file mode 100644 index 0000000..72ad29a --- /dev/null +++ b/hw1/main.cpp @@ -0,0 +1,23 @@ +#include "allocator.h" + +#include + +int main() +{ + Allocator obj1; + obj1.makeAllocator(10); + char* plc1 = obj1.alloc(3); + plc1[0] = 'a'; + plc1[1] = 'b'; + plc1[2] = 'c'; + + std::cout << *plc1 << std::endl; + + obj1.reset(); + obj1.makeAllocator(1000000000); + if (NULL == obj1.alloc(1000000000000000)) { + std::cout << "OK" << std::endl; + } + + return 0; +} diff --git a/hw1/test.cpp b/hw1/test.cpp new file mode 100644 index 0000000..faad2d2 --- /dev/null +++ b/hw1/test.cpp @@ -0,0 +1,47 @@ +#include +#include + +#include "allocator.h" + +TEST(EntryDataTest1, SubTest1) { + Allocator obj1; + obj1.makeAllocator(10); + char* plc1 = obj1.alloc(3); + *plc1 = 'h'; + *(plc1 + 1) = 's'; + *(plc1 + 2) = 'e'; + + ASSERT_TRUE(*(plc1 + 1) == 's'); + ASSERT_FALSE(*(plc1 + 2) == 's'); + ASSERT_TRUE(*plc1 == 'h'); +}; + +TEST(EntryDataTest1, SubTest2) { + Allocator obj1; + obj1.makeAllocator(10); + char* error_test_var = obj1.alloc(11); + + ASSERT_TRUE(NULL == error_test_var); +}; + +TEST(EntryDataTest1, SubTest3) { + Allocator obj1; + obj1.makeAllocator(1000); + char* ukz1 = obj1.alloc(2); + char* ukz2 = obj1.alloc(3); + + *ukz1 = 's'; + *(ukz1 + 1) = 's'; + *ukz2 = 'f'; + *(ukz2 + 1) = 'f'; + + ASSERT_TRUE(*(ukz1 + 1) == 's'); + ASSERT_TRUE(*ukz2 == 'f'); + ASSERT_TRUE(*(ukz1 + 2) == 'f'); +}; + +int main(int argc, char** argv) { + testing::InitGoogleTest(&argc, argv); + + return RUN_ALL_TESTS(); +}