Skip to content

Commit

Permalink
Use uint32_t instead of int for the dynamic API
Browse files Browse the repository at this point in the history
  • Loading branch information
[email protected] committed Apr 3, 2014
1 parent d2ad115 commit 3fced38
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 16 deletions.
13 changes: 7 additions & 6 deletions internal/cld2_dynamic_data_loader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// limitations under the License.

#include <assert.h>
#include <stdint.h>
#include <stdio.h>
#include <iostream>
#include <fstream>
Expand Down Expand Up @@ -40,7 +41,7 @@ CLD2DynamicData::FileHeader* loadHeaderFromFile(const char* fileName) {
}

CLD2DynamicData::FileHeader* loadHeaderFromRaw(const void* basePointer,
const int length) {
const uint32_t length) {
return loadInternal(NULL, basePointer, length);
}

Expand All @@ -52,7 +53,7 @@ CLD2DynamicData::FileHeader* loadHeaderFromRaw(const void* basePointer,
memcpy(&(header->field), (((char*)(basePointer)) + bytesRead), 4);\
bytesRead += 4;\
}
CLD2DynamicData::FileHeader* loadInternal(FILE* inFile, const void* basePointer, const int length) {
CLD2DynamicData::FileHeader* loadInternal(FILE* inFile, const void* basePointer, const uint32_t length) {
const bool sourceIsFile = (inFile != NULL);
int bytesRead = 0;
CLD2DynamicData::FileHeader* header = new CLD2DynamicData::FileHeader;
Expand Down Expand Up @@ -140,7 +141,7 @@ CLD2DynamicData::FileHeader* loadInternal(FILE* inFile, const void* basePointer,
}

void unloadDataFile(CLD2::ScoringTables** scoringTables,
void** mmapAddress, int* mmapLength) {
void** mmapAddress, uint32_t* mmapLength) {
CLD2DynamicDataLoader::unloadDataRaw(scoringTables);
munmap(*mmapAddress, *mmapLength);
*mmapAddress = NULL;
Expand All @@ -157,7 +158,7 @@ void unloadDataRaw(CLD2::ScoringTables** scoringTables) {
}

CLD2::ScoringTables* loadDataFile(const char* fileName,
void** mmapAddressOut, int* mmapLengthOut) {
void** mmapAddressOut, uint32_t* mmapLengthOut) {
CLD2DynamicData::FileHeader* header = loadHeaderFromFile(fileName);
if (header == NULL) {
return NULL;
Expand All @@ -175,12 +176,12 @@ CLD2::ScoringTables* loadDataFile(const char* fileName,
return loadDataInternal(header, mapped, header->totalFileSizeBytes);
}

CLD2::ScoringTables* loadDataRaw(const void* basePointer, const int length) {
CLD2::ScoringTables* loadDataRaw(const void* basePointer, const uint32_t length) {
CLD2DynamicData::FileHeader* header = loadHeaderFromRaw(basePointer, length);
return loadDataInternal(header, basePointer, length);
}

CLD2::ScoringTables* loadDataInternal(CLD2DynamicData::FileHeader* header, const void* basePointer, const int length) {
CLD2::ScoringTables* loadDataInternal(CLD2DynamicData::FileHeader* header, const void* basePointer, const uint32_t length) {
// 1. UTF8 Object
const CLD2::uint8* state_table = static_cast<const CLD2::uint8*>(basePointer) +
header->startOf_utf8PropObj_state_table;
Expand Down
13 changes: 7 additions & 6 deletions internal/cld2_dynamic_data_loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#ifndef CLD2_INTERNAL_CLD2_DYNAMIC_DATA_LOADER_H_
#define CLD2_INTERNAL_CLD2_DYNAMIC_DATA_LOADER_H_

#include <stdint.h>
#include "scoreonescriptspan.h"
#include "cld2_dynamic_data.h"

Expand All @@ -29,11 +30,11 @@ CLD2DynamicData::FileHeader* loadHeaderFromFile(const char* fileName);
// The header returned is dynamically allocated; you must 'delete' the array
// of TableHeaders as well as the returned FileHeader* when done.
CLD2DynamicData::FileHeader* loadHeaderFromRaw(
const void* basePointer, const int length);
const void* basePointer, const uint32_t length);

// Not for public consumption.
CLD2DynamicData::FileHeader* loadInternal(
FILE* inFile, const void* basePointer, const int length);
FILE* inFile, const void* basePointer, const uint32_t length);

// Load data directly into a ScoringTables structure using a private, read-only
// mmap and return the newly-allocated structure.
Expand All @@ -44,16 +45,16 @@ CLD2DynamicData::FileHeader* loadInternal(
// It is up to the caller to delete the data at a later time using
// unloadData(...).
CLD2::ScoringTables* loadDataFile(const char* fileName,
void** mmapAddressOut, int* mmapLengthOut);
void** mmapAddressOut, uint32_t* mmapLengthOut);

// Load data directly into a ScoringTables structure from an arbitrary region
// of memory, which is assumed to be a pointer to an mmap-ed region of memory
// backed by a valid data file that could alternatively be read (if access
// were allowed or desired) using loadDataFile(...).
CLD2::ScoringTables* loadDataRaw(const void* basePointer, const int length);
CLD2::ScoringTables* loadDataRaw(const void* basePointer, const uint32_t length);

// Not for public consumption.
CLD2::ScoringTables* loadDataInternal(CLD2DynamicData::FileHeader* header, const void* basePointer, const int length);
CLD2::ScoringTables* loadDataInternal(CLD2DynamicData::FileHeader* header, const void* basePointer, const uint32_t length);

// Given pointers to the data from a previous invocation of loadDataFile,
// unloads the data safely - freeing and deleting any malloc'd/new'd objects.
Expand All @@ -66,7 +67,7 @@ CLD2::ScoringTables* loadDataInternal(CLD2DynamicData::FileHeader* header, const
// is an unfortunate mixture of new and malloc involved in building the
// in-memory represtation of the data.
void unloadDataFile(CLD2::ScoringTables** scoringTables,
void** mmapAddress, int* mmapLength);
void** mmapAddress, uint32_t* mmapLength);

// Given a pointer to the data from a previous invocation of loadDataRaw,
// unloads the data safely just like unloadDataFile does. This method doesn't
Expand Down
2 changes: 1 addition & 1 deletion internal/cld2_dynamic_data_tool.cc
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ Usage:\n\

if (mode == 1 || mode == 2) { // dump || verify (so perform verification)
void* mmapAddress = NULL;
int mmapLength = 0;
uint32_t mmapLength = 0;
CLD2::ScoringTables* loadedData = CLD2DynamicDataLoader::loadDataFile(fileName, &mmapAddress, &mmapLength);

if (loadedData == NULL) {
Expand Down
5 changes: 3 additions & 2 deletions internal/compact_lang_det_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
// Updated 2014.01 for dual table lookup
//

#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <string>
Expand Down Expand Up @@ -88,7 +89,7 @@ extern const short kAvgDeltaOctaScore[];
static bool dataSourceIsFile = false;
static ScoringTables* dynamicTables = NULL;
static void* mmapAddress = NULL;
static int mmapLength = 0;
static uint32_t mmapLength = 0;

bool isDataLoaded() { return dynamicDataLoaded; }

Expand All @@ -102,7 +103,7 @@ extern const short kAvgDeltaOctaScore[];
dynamicDataLoaded = true;
};

void loadDataFromRawAddress(const void* rawAddress, const int length) {
void loadDataFromRawAddress(const void* rawAddress, const uint32_t length) {
if (isDataLoaded()) {
unloadData();
}
Expand Down
3 changes: 2 additions & 1 deletion public/compact_lang_det.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
#ifndef I18N_ENCODINGS_CLD2_PUBLIC_COMPACT_LANG_DET_H_
#define I18N_ENCODINGS_CLD2_PUBLIC_COMPACT_LANG_DET_H_

#include <stdint.h>
#include <vector>
#include "../internal/lang_script.h" // For Language

Expand Down Expand Up @@ -318,7 +319,7 @@ void loadDataFromFile(const char* fileName);
// in from the specified location again (even if it has not changed).
// WARNING: Before calling one of the provided "loadData" methods, language
// detection will always fail and will always return the unknown language.
void loadDataFromRawAddress(const void* rawAddress, const int length);
void loadDataFromRawAddress(const void* rawAddress, const uint32_t length);

// If compiled with dynamic mode, unload the data that was previously loaded
// via loadDataFromFile() or loadDataFromRawAddress().
Expand Down

0 comments on commit 3fced38

Please sign in to comment.