Skip to content

Commit

Permalink
Use vector instead of CFMutableArrayRef; fix fsevents#52
Browse files Browse the repository at this point in the history
  • Loading branch information
孝达 committed Jul 22, 2015
1 parent 619898f commit 231af0d
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 31 deletions.
7 changes: 4 additions & 3 deletions fsevents.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "CoreFoundation/CoreFoundation.h"
#include "CoreServices/CoreServices.h"
#include <iostream>
#include <vector>

#include "src/storage.cc"
namespace fse {
Expand Down Expand Up @@ -45,7 +46,7 @@ namespace fse {

// Common
CFArrayRef paths;
CFMutableArrayRef events;
std::vector<fse_event*> *events;
static void Initialize(v8::Handle<v8::Object> exports);

// methods.cc - exposed
Expand All @@ -61,7 +62,7 @@ using namespace fse;
FSEvents::FSEvents(const char *path, NanCallback *handler): handler(handler) {
CFStringRef dirs[] = { CFStringCreateWithCString(NULL, path, kCFStringEncodingUTF8) };
paths = CFArrayCreate(NULL, (const void **)&dirs, 1, NULL);
events = CFArrayCreateMutable(NULL, 0, &FSEventArrayCallBacks);
events = new std::vector<fse_event*>();
threadloop = NULL;
lockingStart();
}
Expand All @@ -72,7 +73,7 @@ FSEvents::~FSEvents() {
handler = NULL;

CFRelease(paths);
CFRelease(events);
delete events;
}

#ifndef kFSEventStreamEventFlagItemCreated
Expand Down
7 changes: 4 additions & 3 deletions src/async.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@ void async_propagate(uv_async_t *async) {
char pathbuf[1024];
const char *pathptr = NULL;
fse->lock();
cnt = CFArrayGetCount(fse->events);
cnt = fse->events->size();
for (idx=0; idx<cnt; idx++) {
event = (fse_event *)CFArrayGetValueAtIndex(fse->events, idx);
event = fse->events->at(idx);
if (event == NULL) continue;
pathptr = CFStringGetCStringPtr(event->path, kCFStringEncodingUTF8);
if (!pathptr) CFStringGetCString(event->path, pathbuf, 1024, kCFStringEncodingUTF8);
fse->emitEvent(pathptr ? pathptr : pathbuf, event->flags, event->id);
delete event;
}
if (cnt>0) CFArrayRemoveAllValues(fse->events);
if (cnt>0) fse->events->clear();
fse->unlock();
}

Expand Down
33 changes: 13 additions & 20 deletions src/storage.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,18 @@ struct fse_event {
UInt64 id;
UInt32 flags;
CFStringRef path;

fse_event(CFStringRef eventPath, UInt32 eventFlag, UInt64 eventId) {
this->path = eventPath;
this->flags = eventFlag;
this->id = eventId;
if (this->path)
CFRetain(this->path);
}

~fse_event(){
if (this->path)
CFRelease(this->path);
}
};
typedef struct fse_event fse_event;

const void * FSEventRetain(CFAllocatorRef allocator, const void * ptr) {
if (ptr == NULL) return NULL;
fse_event * orig = (fse_event * ) ptr;
fse_event * copy = (fse_event * ) CFAllocatorAllocate(allocator, sizeof(fse_event), 0);
copy->id = orig->id;
copy->flags = orig->flags;
copy->path = orig->path;
CFRetain(copy->path);
return copy;
}
void FSEventRelease(CFAllocatorRef allocator, const void * ptr) {
if (ptr == NULL) return;
fse_event * evt = (fse_event * ) ptr;
CFRelease(evt->path);
CFAllocatorDeallocate(allocator, evt);
}
const CFArrayCallBacks FSEventArrayCallBacks = {
0, FSEventRetain, FSEventRelease, 0, 0
};
11 changes: 6 additions & 5 deletions src/thread.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,13 @@ void HandleStreamEvents(ConstFSEventStreamRef stream, void *ctx, size_t numEvent
FSEvents * fse = (FSEvents *)ctx;
size_t idx;
fse->lock();
fse_event event;
for (idx=0; idx < numEvents; idx++) {
event.path = (CFStringRef)CFArrayGetValueAtIndex((CFArrayRef)eventPaths, idx);
event.flags = eventFlags[idx];
event.id = eventIds[idx];
CFArrayAppendValue(fse->events, &event);
fse_event *event = new fse_event(
(CFStringRef)CFArrayGetValueAtIndex((CFArrayRef)eventPaths, idx),
eventFlags[idx],
eventIds[idx]
);
fse->events->push_back(event);
}
fse->asyncTrigger();
fse->unlock();
Expand Down

0 comments on commit 231af0d

Please sign in to comment.