Skip to content

Commit

Permalink
Merge pull request #209 from alliedmodders/sharing
Browse files Browse the repository at this point in the history
Refactor some common code into a shared library.
  • Loading branch information
dvander committed May 9, 2018
2 parents 6d42be3 + d2dae67 commit 9efbabe
Show file tree
Hide file tree
Showing 12 changed files with 512 additions and 307 deletions.
10 changes: 10 additions & 0 deletions AMBuildScript
Expand Up @@ -261,9 +261,11 @@ class SourcePawn(object):
self.spcomp = {}
self.libspcomp2 = {}
self.zlib = {}
self.libsmx = None

def BuildSpcomp(self):
self.EnsureZlib()
self.EnsureLibSmx()
for arch in self.root.archs:
spcomp = self.BuildForArch('compiler/AMBuilder', arch)
self.spcomp[arch] = spcomp
Expand Down Expand Up @@ -293,6 +295,14 @@ class SourcePawn(object):
self.zlib[arch] = self.BuildForArch("third_party/zlib/AMBuilder", arch)
self.included_zlib = True

def EnsureLibSmx(self):
if self.libsmx is not None:
return
self.libsmx = {}
for arch in self.root.archs:
lib = self.BuildForArch('libsmx/AMBuilder', arch)
self.libsmx[arch] = lib

def BuildForArch(self, scripts, arch):
new_vars = copy.copy(self.vars)
new_vars['arch'] = arch
Expand Down
3 changes: 2 additions & 1 deletion compiler/AMBuilder
Expand Up @@ -10,6 +10,7 @@ compiler.includes += [
os.path.join(builder.currentSourcePath, '..', 'third_party'),
os.path.join(builder.buildPath, 'includes'),
os.path.join(builder.buildPath, builder.buildFolder),
os.path.join(builder.currentSourcePath, '..'),
]

if compiler.like('gcc'):
Expand Down Expand Up @@ -67,7 +68,6 @@ binary.sources += [
'scmemfil.cpp',
'sctracker.cpp',
'scvars.cpp',
'smx-builder.cpp',
'sp_symhash.cpp',
'types.cpp',
'expression-parsing.cpp',
Expand All @@ -78,6 +78,7 @@ if builder.target.platform == 'linux' and not compiler.like('emscripten'):
binary.sources.append('binreloc.c')

binary.compiler.linkflags[0:0] = [
SP.libsmx[arch].binary,
SP.zlib[arch],
]

Expand Down
102 changes: 0 additions & 102 deletions compiler/memory-buffer.h

This file was deleted.

24 changes: 12 additions & 12 deletions compiler/sc6.cpp
Expand Up @@ -40,8 +40,9 @@
#include <smx/smx-v1.h>
#include <smx/smx-v1-opcodes.h>
#include <zlib/zlib.h>
#include "smx-builder.h"
#include "memory-buffer.h"
#include "libsmx/smx-builder.h"
#include "shared/byte-buffer.h"
#include "shared/string-pool.h"
#include "types.h"

using namespace sp;
Expand Down Expand Up @@ -815,7 +816,7 @@ typedef SmxListSection<sp_file_pubvars_t> SmxPubvarSection;
typedef SmxBlobSection<sp_file_data_t> SmxDataSection;
typedef SmxBlobSection<sp_file_code_t> SmxCodeSection;

static void assemble_to_buffer(MemoryBuffer *buffer, void *fin)
static void assemble_to_buffer(SmxByteBuffer *buffer, void *fin)
{
StringPool pool;
SmxBuilder builder;
Expand Down Expand Up @@ -949,7 +950,7 @@ static void assemble_to_buffer(MemoryBuffer *buffer, void *fin)
builder.write(buffer);
}

static void splat_to_binary(const char *binfname, void *bytes, size_t size)
static void splat_to_binary(const char *binfname, const void *bytes, size_t size)
{
// Note: error 161 will setjmp(), which skips destructors :(
FILE *fp = fopen(binfname, "wb");
Expand All @@ -967,25 +968,24 @@ static void splat_to_binary(const char *binfname, void *bytes, size_t size)

void assemble(const char *binfname, void *fin)
{
MemoryBuffer buffer;
SmxByteBuffer buffer;
assemble_to_buffer(&buffer, fin);

// Buffer compression logic.
sp_file_hdr_t *header = (sp_file_hdr_t *)buffer.bytes();
size_t region_size = header->imagesize - header->dataoffs;
size_t zbuf_max = compressBound(region_size);
Bytef *zbuf = (Bytef *)malloc(zbuf_max);
UniquePtr<Bytef[]> zbuf = MakeUnique<Bytef[]>(zbuf_max);

uLong new_disksize = zbuf_max;
int err = compress2(
zbuf,
zbuf.get(),
&new_disksize,
(Bytef *)(buffer.bytes() + header->dataoffs),
region_size,
Z_BEST_COMPRESSION
);
if (err != Z_OK) {
free(zbuf);
pc_printf("Unable to compress, error %d\n", err);
pc_printf("Falling back to no compression.\n");
splat_to_binary(binfname, buffer.bytes(), buffer.size());
Expand All @@ -995,9 +995,9 @@ void assemble(const char *binfname, void *fin)
header->disksize = new_disksize + header->dataoffs;
header->compression = SmxConsts::FILE_COMPRESSION_GZ;

buffer.rewind(header->dataoffs);
buffer.write(zbuf, new_disksize);
free(zbuf);
ByteBuffer new_buffer;
new_buffer.writeBytes(buffer.bytes(), header->dataoffs);
new_buffer.writeBytes(zbuf.get(), new_disksize);

splat_to_binary(binfname, buffer.bytes(), buffer.size());
splat_to_binary(binfname, new_buffer.bytes(), new_buffer.size());
}
146 changes: 0 additions & 146 deletions compiler/string-pool.h

This file was deleted.

0 comments on commit 9efbabe

Please sign in to comment.