Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions hw1/Makefile
Original file line number Diff line number Diff line change
@@ -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
29 changes: 29 additions & 0 deletions hw1/allocator.cpp
Original file line number Diff line number Diff line change
@@ -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;
}
22 changes: 22 additions & 0 deletions hw1/allocator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <stdint.h>
#include <cstddef>

#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
23 changes: 23 additions & 0 deletions hw1/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include "allocator.h"

#include <iostream>

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;
}
47 changes: 47 additions & 0 deletions hw1/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include <iostream>
#include <gtest/gtest.h>

#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();
}