Skip to content

Commit

Permalink
Merge pull request #39 from dryman/OPIC-34
Browse files Browse the repository at this point in the history
Opic 34 Restructure codebase
  • Loading branch information
dryman authored Mar 26, 2017
2 parents d6bab21 + 73059df commit a219873
Show file tree
Hide file tree
Showing 46 changed files with 396 additions and 1,266 deletions.
6 changes: 5 additions & 1 deletion Makefile.am
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
SUBDIRS = src
SUBDIRS = opic

nobase_include_HEADERS = \
opic/op_malloc.h \
opic/demomalloc.h
6 changes: 3 additions & 3 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ AC_INIT([OPIC],[0.3])
AC_CONFIG_SRCDIR([README.md])
AC_CONFIG_AUX_DIR([build-aux])
AC_CONFIG_MACRO_DIR([m4])
AM_CONFIG_HEADER([src/config.h])
AM_CONFIG_HEADER([config.h])

AM_INIT_AUTOMAKE([-Wall -Werror foreign])
AM_PROG_AR
Expand Down Expand Up @@ -46,8 +46,8 @@ PKG_CHECK_MODULES([cmocka], [cmocka >= 1.0.1])

AC_CONFIG_FILES([
Makefile
src/Makefile
src/opic/malloc/Makefile
opic/Makefile
opic/malloc/Makefile
])

AC_OUTPUT
15 changes: 15 additions & 0 deletions opic/Makefile.am
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
SUBDIRS = malloc

AUTOMAKE_OPTIONS = subdir-objects
AM_CPPFLAGS = -I$(top_srcdir)

lib_LTLIBRARIES = libdemomalloc.la

libdemomalloc_la_SOURCES = \
demomalloc.c \
malloc/allocator.c \
malloc/deallocator.c \
malloc/init_helper.c \
malloc/lookup_helper.c

libdemomalloc_la_LIBADD = @atomic_LIBS@
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
110 changes: 110 additions & 0 deletions opic/demomalloc.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/* demomalloc.c ---
*
* Filename: demomalloc.c
* Description:
* Author: Felix Chern
* Maintainer:
* Copyright: (c) 2017 Felix Chern
* Created: Sun Mar 26 11:43:56 2017 (-0700)
* Version:
* Package-Requires: ()
* Last-Updated:
* By:
* Update #: 0
* URL:
* Doc URL:
* Keywords:
* Compatibility:
*
*/

/* Commentary:
*
*
*
*/

/* Change Log:
*
*
*/

/* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

/* Code: */

#include <stdatomic.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include "opic/op_malloc.h"
#include "opic/demomalloc.h"

static atomic_flag lock = ATOMIC_FLAG_INIT;
static OPHeap* heap;

static
OPHeap* GetOPHeap()
{
while (true)
{
if (heap)
return heap;
if (atomic_flag_test_and_set_explicit(&lock, memory_order_acquire))
continue;
OPHeapNew(&heap);
atomic_flag_clear_explicit(&lock, memory_order_release);
}
}

void*
OPDemoMalloc(size_t size)
{
OPHeap* heap = GetOPHeap();
return OPMallocRaw(heap, size);
}

void*
OPDemoCalloc(size_t num, size_t size)
{
OPHeap* heap = GetOPHeap();
return OPCallocRaw(heap, num, size);
}

void*
OPDemoRealloc(void* addr, size_t size)
{
OPHeap* heap;
void* new_addr;
heap = ObtainOPHeap(addr);
// NOTE: for sblob/hblob we might find the new size still fits in the blob.
// We can do this optimization later.
new_addr = OPMallocRaw(heap, size);
if (new_addr)
{
memcpy(new_addr, addr, size);
OPDealloc(addr);
}
return new_addr;
}

void
OPDemoFree(void* addr)
{
OPDealloc(addr);
}

/* demomalloc.c ends here */
55 changes: 33 additions & 22 deletions src/opic/malloc/huge_page.h → opic/demomalloc.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/* huge_page.h ---
/* demomalloc.h ---
*
* Filename: huge_page.h
* Filename: demomalloc.h
* Description:
* Author: Felix Chern
* Maintainer:
* Copyright: (c) 2016 Felix Chern
* Created: Sat Oct 22, 2016
* Copyright: (c) 2017 Felix Chern
* Created: Sun Mar 26 12:07:34 2017 (-0700)
* Version:
* Package-Requires: ()
* Last-Updated:
Expand Down Expand Up @@ -45,35 +45,46 @@

/* Code: */

#ifndef HUGE_PAGE_H
#define HUGE_PAGE_H 1
#ifndef OPIC_DEMOMALLOC_H
#define OPIC_DEMOMALLOC_H 1

#include <stdint.h>
#include <stdbool.h>
#include <stdatomic.h>
#include "opic/common/op_macros.h"
#include "opic/common/op_assert.h"
#include "opic/common/op_log.h"
#include "malloc_internal.h"
#include <assert.h>
#include "opic/op_malloc.h"

OP_BEGIN_DECLS

void* OPDemoMalloc(size_t size)
__attribute__((malloc));

HugePage* HugePageInit(void* addr, Magic magic)
__attribute__((nonnull));
void* OPDemoCalloc(size_t num, size_t size)
__attribute__((malloc));

UnarySpan* ObtainUSpan(HugePage* self,
Magic magic,
unsigned int span_cnt)
__attribute__((nonnull));
void* OPDemoRealloc(void* addr, size_t size)
__attribute__((malloc));

FreeStatus HugePageFree(HugePage* self, void* addr)
__attribute__((nonnull));
void OPDemoFree(void* addr);

/*
* Somehow I cannot compile the code below on OSX.
* Since replacing malloc is not the major goal, I'll just leave it here.
*/

/*
void* malloc(size_t size)
__attribute__((weak, alias("OPDemoMalloc"))) __attribute__((malloc));
void* calloc(size_t num, size_t size)
__attribute__((weak, alias("OPDemoCalloc"))) __attribute__((malloc));
void* realloc(void* addr, size_t size)
__attribute__((weak, alias("OPDemoRealloc"))) __attribute__((malloc));
void free(void* addr)
__attribute__((weak, alias("OPDemoFree")));
*/

OP_END_DECLS

#endif

/* huge_page.h ends here */
/* demomalloc.h ends here */
17 changes: 6 additions & 11 deletions src/opic/malloc/Makefile.am → opic/malloc/Makefile.am
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
AUTOMAKE_OPTIONS = subdir-objects
AM_CPPFLAGS = -I$(top_srcdir)/src
AM_CFLAGS = @log4c_CFLAGS@ @cmocka_CFLAGS@
AM_CPPFLAGS = -I$(top_srcdir)
AM_CFLAGS = @cmocka_CFLAGS@

TESTS = lookup_helper_test init_helper_test allocator_test \
deallocator_test
Expand All @@ -10,8 +9,7 @@ check_PROGRAMS = lookup_helper_test init_helper_test allocator_test \
lookup_helper_test_SOURCES = \
lookup_helper_test.c \
lookup_helper.c \
init_helper.c \
../common/op_log.c
init_helper.c

lookup_helper_test_LDADD = @log4c_LIBS@ @PTHREAD_LIBS@ \
@cmocka_LIBS@ @atomic_LIBS@
Expand All @@ -20,8 +18,7 @@ lookup_helper_test_LDFLAGS = -static
init_helper_test_SOURCES = \
init_helper.c \
init_helper_test.c \
lookup_helper.c \
../common/op_log.c
lookup_helper.c

init_helper_test_LDADD = @log4c_LIBS@ @PTHREAD_LIBS@ \
@cmocka_LIBS@ @atomic_LIBS@
Expand All @@ -32,8 +29,7 @@ allocator_test_SOURCES = \
allocator_test.c \
deallocator.c \
init_helper.c \
lookup_helper.c \
../common/op_log.c
lookup_helper.c

allocator_test_LDADD = @log4c_LIBS@ @PTHREAD_LIBS@ \
@cmocka_LIBS@ @atomic_LIBS@
Expand All @@ -44,8 +40,7 @@ deallocator_test_SOURCES = \
deallocator.c \
deallocator_test.c \
init_helper.c \
lookup_helper.c \
../common/op_log.c
lookup_helper.c

deallocator_test_LDADD = @log4c_LIBS@ @PTHREAD_LIBS@ \
@cmocka_LIBS@ @atomic_LIBS@
Expand Down
Loading

0 comments on commit a219873

Please sign in to comment.