Skip to content

Commit

Permalink
Merge pull request #18 from mentebinaria/maProc-dev
Browse files Browse the repository at this point in the history
Version maProc 0.2.5
  • Loading branch information
rem0obb committed Jul 14, 2022
2 parents 81f4d06 + d202e4a commit d60f5a8
Show file tree
Hide file tree
Showing 14 changed files with 3,010 additions and 315 deletions.
118 changes: 71 additions & 47 deletions src/arena.cpp
Original file line number Diff line number Diff line change
@@ -1,126 +1,150 @@
#include "src/include/arena.hpp"

#include <stdexcept>

/**
* if p_overwrite = true (default) Arena overwrite spaces alocated
*
* */
Arena::Arena ( unsigned int p_size, bool p_overwrite ) : m_size ( p_size ),
m_amount ( m_size ),
m_overwrite ( p_overwrite )
Arena::Arena(unsigned int p_size, unsigned int p_limit, bool p_overwrite) : m_size(p_size),
m_amount(m_size),
m_limit(p_limit),
m_overwrite(p_overwrite)
{
m_mem = new char[m_size];
m_ptr = m_mem;
m_head = m_mem;
}

Arena::Arena() : m_mem(nullptr),
m_size(0),
m_amount(0),
m_limit(0),
m_head(nullptr),
m_overwrite(0)
{
}

/*
* avoid copy arena, greater than the heap, but it loses performance
* */
Arena Arena::operator= ( const Arena &fast )
Arena Arena::operator=(const Arena &fast)
{
delete [] m_mem;
delete[] m_mem;

m_mem = new char[fast.m_size];
memcpy ( m_mem, fast.m_mem, fast.m_size );
memcpy(m_mem, fast.m_mem, fast.m_size);
m_size = fast.m_size;
m_ptr = fast.m_ptr;
m_head = fast.m_head;
m_amount = fast.m_amount;
m_overwrite = fast.m_overwrite;
m_limit = m_amount;

return *this;
}


/*
* avoid copy arena, greater than the heap, but it loses performance
*/
Arena::Arena ( const Arena &fast )
*/
Arena::Arena(const Arena &fast)
{
delete [] m_mem;

m_mem = new char[fast.m_size];
memcpy ( m_mem, fast.m_mem, fast.m_size );
memcpy(m_mem, fast.m_mem, fast.m_size);
m_size = fast.m_size;
m_ptr = fast.m_ptr;
m_head = fast.m_head;
m_amount = fast.m_amount;
m_overwrite = fast.m_overwrite;
m_limit = m_amount;
}

Arena::~Arena()
{
delete [] m_mem;
m_ptr = m_mem;
delete[] m_mem;
m_mem = nullptr;
m_head = m_mem;
}

/*
* requestes block in arena passing desired size
* */
void *Arena::req ( unsigned int p_amount )
void *Arena::req(unsigned int p_amount)
{
char *block = nullptr;

if ( p_amount <= m_amount )
if (p_amount <= m_amount)
{
block = ( char * ) m_ptr;
m_ptr += p_amount;
m_amount -= p_amount;
if (p_amount < m_limit && m_overwrite && m_size > m_limit)
{
realloc(m_limit);
goto new_block;
}
else
goto new_block;
}
else if ( p_amount > m_size )
else if (p_amount > m_size)
{
dell();
mmem ( p_amount );
block = ( char * ) m_ptr;
m_ptr += p_amount;
m_amount -= p_amount;
realloc(p_amount);
goto new_block;
}
else
{
if ( m_overwrite )
if (m_overwrite)
{
dell();
block = ( char * ) m_ptr;
m_ptr += p_amount;
m_amount -= p_amount;

goto new_block;
}
else
throw std::runtime_error ( "Arena full" );
throw std::runtime_error("Arena : Arena is full");
}

new_block:
char *block = const_cast<char*>(m_head);
m_head += p_amount;
m_amount -= p_amount;

return block;
}

/* (m_ptr)
/* (m_head)
* ↓
* (m_mem) -> [ ][ ][ ][ ] ...
*
* return pointer head for initial memory
* */
void Arena::dell()
{
m_ptr = m_mem;
m_head = m_mem;
m_amount = m_size;
}

/*
* Delete memory
* */
void Arena::erase()
{
delete[] m_mem;
m_mem = nullptr;
m_head = m_mem;
m_size = 0;
m_amount = m_size;
m_overwrite = true;
}

/*
* return amount free space in arena
* */
unsigned int Arena::afree()
unsigned int Arena::fquantity()
{
return m_amount;
}

/*
* ... [ ][ ][ ] <- [del] - [alocate] -> [ ][ ][ ][ ] +++
* ... [ ][ ][ ] <- [delete] - [alocate more] -> [ ][ ][ ][ ] +++
* */
void Arena::mmem ( unsigned int p_amount )
void Arena::realloc(unsigned int p_amount)
{
if ( m_mem == nullptr )
if (m_mem != nullptr)
delete[] m_mem;
else if(p_amount == 0 || p_amount == m_size)
return;

delete [] m_mem;
m_mem = new char[p_amount];
m_ptr = m_mem;
m_head = m_mem;
m_size = p_amount;
m_amount = m_size;
}
}
10 changes: 5 additions & 5 deletions src/gui/about.ui
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>347</width>
<height>298</height>
<width>353</width>
<height>296</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_4">
Expand All @@ -68,7 +68,7 @@
</size>
</property>
<property name="text">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;&lt;span style=&quot; font-size:12pt; font-weight:600; font-style:italic; color:#54932a;&quot;&gt;maProc v0.2.0&lt;/span&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;&lt;span style=&quot; font-size:12pt; font-weight:600; font-style:italic; color:#54932a;&quot;&gt;maProc v0.2.5&lt;/span&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
</widget>
</item>
Expand Down Expand Up @@ -160,8 +160,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>346</width>
<height>298</height>
<width>339</width>
<height>296</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
Expand Down
Loading

0 comments on commit d60f5a8

Please sign in to comment.