2011-07-13 14:25:21 +00:00
|
|
|
/* Copyright (c) 2008-2011, Avian Contributors
|
2008-02-19 18:06:52 +00:00
|
|
|
|
|
|
|
Permission to use, copy, modify, and/or distribute this software
|
|
|
|
for any purpose with or without fee is hereby granted, provided
|
|
|
|
that the above copyright notice and this permission notice appear
|
|
|
|
in all copies.
|
|
|
|
|
|
|
|
There is NO WARRANTY for this software. See license.txt for
|
|
|
|
details. */
|
|
|
|
|
2008-01-10 01:20:36 +00:00
|
|
|
#ifndef ALLOCATOR_H
|
|
|
|
#define ALLOCATOR_H
|
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
|
|
|
|
namespace vm {
|
|
|
|
|
|
|
|
class Allocator {
|
|
|
|
public:
|
2008-04-13 18:15:04 +00:00
|
|
|
virtual void* tryAllocate(unsigned size) = 0;
|
|
|
|
virtual void* allocate(unsigned size) = 0;
|
|
|
|
virtual void free(const void* p, unsigned size) = 0;
|
2008-01-10 01:20:36 +00:00
|
|
|
};
|
|
|
|
|
2011-07-11 16:56:53 +00:00
|
|
|
inline const char*
|
|
|
|
append(Allocator* allocator, const char* a, const char* b, const char* c)
|
|
|
|
{
|
|
|
|
unsigned al = strlen(a);
|
|
|
|
unsigned bl = strlen(b);
|
|
|
|
unsigned cl = strlen(c);
|
|
|
|
char* p = static_cast<char*>(allocator->allocate((al + bl + cl) + 1));
|
|
|
|
memcpy(p, a, al);
|
|
|
|
memcpy(p + al, b, bl);
|
|
|
|
memcpy(p + al + bl, c, cl + 1);
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline const char*
|
|
|
|
append(Allocator* allocator, const char* a, const char* b)
|
|
|
|
{
|
|
|
|
unsigned al = strlen(a);
|
|
|
|
unsigned bl = strlen(b);
|
|
|
|
char* p = static_cast<char*>(allocator->allocate((al + bl) + 1));
|
|
|
|
memcpy(p, a, al);
|
|
|
|
memcpy(p + al, b, bl + 1);
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline const char*
|
|
|
|
copy(Allocator* allocator, const char* a)
|
|
|
|
{
|
|
|
|
unsigned al = strlen(a);
|
|
|
|
char* p = static_cast<char*>(allocator->allocate(al + 1));
|
|
|
|
memcpy(p, a, al + 1);
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2008-01-10 01:20:36 +00:00
|
|
|
} // namespace vm
|
|
|
|
|
|
|
|
#endif//ALLOCATOR_H
|