Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix several memory management bugs #1

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions bitmap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,21 @@ BitmapPoppy::BitmapPoppy(uint64 *bits, uint64 nbits)
l2EntryCount_ = nbits_ >> 11;
basicBlockCount_ = nbits_ / kBasicBlockSize;

assert(posix_memalign((void **) &l1Entries_, kCacheLineSize, l1EntryCount_ * sizeof(uint64)) >= 0);
assert(posix_memalign((void **) &l2Entries_, kCacheLineSize, l2EntryCount_ * sizeof(uint64)) >= 0);
if (posix_memalign((void **) &l1Entries_, kCacheLineSize, l1EntryCount_ * sizeof(uint64)) != 0) {
fprintf(stderr, "allocation of l1Entries_ failed");
exit(1);
}
if (posix_memalign((void **) &l2Entries_, kCacheLineSize, l2EntryCount_ * sizeof(uint64)) != 0) {
fprintf(stderr, "allocation of l2Entries_ failed");
exit(1);
}

uint64 l2Id = 0;
uint64 basicBlockId = 0;

pCount_ = 0;
memset(locCount_, 0, sizeof(locCount_));
loc_.reserve(l1EntryCount_);
locCount_.reserve(l1EntryCount_);

for (uint64 i = 0; i < l1EntryCount_; i++) {
l1Entries_[i] = pCount_;
Expand All @@ -50,8 +57,8 @@ BitmapPoppy::BitmapPoppy(uint64 *bits, uint64 nbits)
l2Entries_[l2Id] = cum;

for (int offset = 0; offset < 30; offset += 10) {
int c = popcountLinear(bits_,
basicBlockId * kWordCountPerBasicBlock,
int c = popcountLinear(bits_,
basicBlockId * kWordCountPerBasicBlock,
kBasicBlockSize);
cum += c;
basicBlockId++;
Expand All @@ -63,15 +70,17 @@ BitmapPoppy::BitmapPoppy(uint64 *bits, uint64 nbits)
if (++l2Id >= l2EntryCount_) break;
}

locCount_[i] = (cum + kLocFreq - 1) / kLocFreq;
assert(locCount_.size() == i);
locCount_.push_back((cum + kLocFreq - 1) / kLocFreq);
pCount_ += cum;
}

basicBlockId = 0;
//fprintf(stdout, "Starting the loop: %ld \n", locCount_[0]);

for (uint64 i = 0; i < l1EntryCount_; i++) {
loc_[i] = new uint32[locCount_[i]];
assert(loc_.size() == i);
loc_.push_back(new uint32[locCount_[i]]);
//fprintf(stdout, "Starting the loop: %ld %ld \n", i, locCount_[i]);
locCount_[i] = 0;

Expand Down Expand Up @@ -124,7 +133,7 @@ uint64 BitmapPoppy::select(uint64 rank)

//struct timeval tv_start, tv_end;
//gettimeofday(&tv_start, NULL);

uint64 l1Id;
for (l1Id = l1EntryCount_ - 1; l1Id >= 0; l1Id--) {
if (l1Entries_[l1Id] < rank) {
Expand Down
15 changes: 11 additions & 4 deletions bitmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ limitations under the License.
#include <string.h>
#define __STDC_FORMAT_MACROS
#include <inttypes.h>
#include <vector>

#include "shared.h"

Expand All @@ -52,10 +53,16 @@ class Bitmap {
uint64 pCount_;
};

class BitmapPoppy: public Bitmap {
class BitmapPoppy final: public Bitmap {
public:
BitmapPoppy(uint64* bits, uint64 nbits);
~BitmapPoppy() {}
~BitmapPoppy() {
for (uint32* p : loc_) {
delete[] p;
}
free(static_cast<void*>(l1Entries_));
free(static_cast<void*>(l2Entries_));
}

uint64 rank(uint64 pos);
uint64 select(uint64 rank);
Expand All @@ -71,8 +78,8 @@ class BitmapPoppy: public Bitmap {
uint64 l1EntryCount_;
uint64 basicBlockCount_;

uint32* loc_[1ULL << 31];
uint64 locCount_[1ULL << 31];
std::vector<uint32*> loc_;
std::vector<uint64> locCount_;

static const int kLocFreq = 8192;
static const int kLocFreqMask = 8191;
Expand Down