wdm 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,72 +1,72 @@
1
- #include "wdm.h"
2
-
3
- #include "memory.h"
4
- #include "entry.h"
5
-
6
- // ---------------------------------------------------------
7
- // Entry user data functions
8
- // ---------------------------------------------------------
9
-
10
- WDM_PEntryUserData
11
- wdm_entry_user_data_new()
12
- {
13
- WDM_PEntryUserData user_data;
14
-
15
- user_data = WDM_ALLOC(WDM_EntryUserData);
16
-
17
- user_data->dir = NULL;
18
- user_data->watch_childeren = FALSE;
19
-
20
- return user_data;
21
- }
22
-
23
- void
24
- wdm_entry_user_data_free(WDM_PEntryUserData user_data)
25
- {
26
- if ( user_data->dir != NULL ) free(user_data->dir);
27
- free(user_data);
28
- }
29
-
30
- // ---------------------------------------------------------
31
- // Entry functions
32
- // ---------------------------------------------------------
33
-
34
- WDM_PEntry
35
- wdm_entry_new()
36
- {
37
- WDM_PEntry entry;
38
-
39
- entry = WDM_ALLOC(WDM_Entry);
40
-
41
- entry->user_data = wdm_entry_user_data_new();
42
- entry->dir_handle = INVALID_HANDLE_VALUE;
43
- entry->next = NULL;
44
-
45
- ZeroMemory(&entry->buffer, WDM_BUFFER_SIZE);
46
- ZeroMemory(&entry->event_container, sizeof(OVERLAPPED));
47
-
48
- return entry;
49
- }
50
-
51
- void
52
- wdm_entry_free(WDM_PEntry entry)
53
- {
54
- if ( entry->dir_handle != INVALID_HANDLE_VALUE ) {
55
- CancelIo(entry->dir_handle); // Stop monitoring changes
56
- CloseHandle(entry->dir_handle);
57
- }
58
- wdm_entry_user_data_free(entry->user_data);
59
- free(entry);
60
- }
61
-
62
- void
63
- wdm_entry_list_free(WDM_PEntry entry)
64
- {
65
- WDM_PEntry tmp;
66
-
67
- while(entry != NULL) {
68
- tmp = entry;
69
- entry = entry->next;
70
- wdm_entry_free(tmp);
71
- }
1
+ #include "wdm.h"
2
+
3
+ #include "memory.h"
4
+ #include "entry.h"
5
+
6
+ // ---------------------------------------------------------
7
+ // Entry user data functions
8
+ // ---------------------------------------------------------
9
+
10
+ WDM_PEntryUserData
11
+ wdm_entry_user_data_new()
12
+ {
13
+ WDM_PEntryUserData user_data;
14
+
15
+ user_data = WDM_ALLOC(WDM_EntryUserData);
16
+
17
+ user_data->dir = NULL;
18
+ user_data->watch_childeren = FALSE;
19
+
20
+ return user_data;
21
+ }
22
+
23
+ void
24
+ wdm_entry_user_data_free(WDM_PEntryUserData user_data)
25
+ {
26
+ if ( user_data->dir != NULL ) free(user_data->dir);
27
+ free(user_data);
28
+ }
29
+
30
+ // ---------------------------------------------------------
31
+ // Entry functions
32
+ // ---------------------------------------------------------
33
+
34
+ WDM_PEntry
35
+ wdm_entry_new()
36
+ {
37
+ WDM_PEntry entry;
38
+
39
+ entry = WDM_ALLOC(WDM_Entry);
40
+
41
+ entry->user_data = wdm_entry_user_data_new();
42
+ entry->dir_handle = INVALID_HANDLE_VALUE;
43
+ entry->next = NULL;
44
+
45
+ ZeroMemory(&entry->buffer, WDM_BUFFER_SIZE);
46
+ ZeroMemory(&entry->event_container, sizeof(OVERLAPPED));
47
+
48
+ return entry;
49
+ }
50
+
51
+ void
52
+ wdm_entry_free(WDM_PEntry entry)
53
+ {
54
+ if ( entry->dir_handle != INVALID_HANDLE_VALUE ) {
55
+ CancelIo(entry->dir_handle); // Stop monitoring changes
56
+ CloseHandle(entry->dir_handle);
57
+ }
58
+ wdm_entry_user_data_free(entry->user_data);
59
+ free(entry);
60
+ }
61
+
62
+ void
63
+ wdm_entry_list_free(WDM_PEntry entry)
64
+ {
65
+ WDM_PEntry tmp;
66
+
67
+ while(entry != NULL) {
68
+ tmp = entry;
69
+ entry = entry->next;
70
+ wdm_entry_free(tmp);
71
+ }
72
72
  }
@@ -1,47 +1,47 @@
1
- #include <Windows.h>
2
- #include <ruby.h>
3
-
4
- #ifndef WDM_ENTRY_H
5
- #define WDM_ENTRY_H
6
-
7
- #ifdef __cplusplus
8
- extern "C" {
9
- #endif // __cplusplus
10
-
11
- // ---------------------------------------------------------
12
- // Types
13
- // ---------------------------------------------------------
14
-
15
- typedef struct {
16
- LPWSTR dir; // Name of directory to watch
17
- VALUE callback; // Proc object to call when there are changes
18
- BOOL watch_childeren; // Watch sub-directories
19
- DWORD flags; // Flags for the type of changes to report
20
- } WDM_EntryUserData, *WDM_PEntryUserData;
21
-
22
- typedef struct WDM_Entry {
23
- WDM_PEntryUserData user_data; // User-supplied data
24
- HANDLE dir_handle; // IO handle of the directory
25
- BYTE buffer[WDM_BUFFER_SIZE]; // Buffer for the results
26
- OVERLAPPED event_container; // Async IO event container
27
- struct WDM_Entry* next; // Well, this is a linked list, so this is self-explanatory :)
28
- } WDM_Entry, *WDM_PEntry;
29
-
30
- // ---------------------------------------------------------
31
- // Prototypes
32
- // ---------------------------------------------------------
33
-
34
- WDM_PEntryUserData wdm_entry_user_data_new();
35
- void wdm_entry_user_data_free(WDM_PEntryUserData);
36
-
37
- WDM_PEntry wdm_entry_new();
38
- void wdm_entry_free(WDM_PEntry);
39
- void wdm_entry_list_free(WDM_PEntry);
40
-
41
- // ---------------------------------------------------------
42
-
43
- #ifdef __cplusplus
44
- }
45
- #endif // __cplusplus
46
-
1
+ #include <Windows.h>
2
+ #include <ruby.h>
3
+
4
+ #ifndef WDM_ENTRY_H
5
+ #define WDM_ENTRY_H
6
+
7
+ #ifdef __cplusplus
8
+ extern "C" {
9
+ #endif // __cplusplus
10
+
11
+ // ---------------------------------------------------------
12
+ // Types
13
+ // ---------------------------------------------------------
14
+
15
+ typedef struct {
16
+ LPWSTR dir; // Name of directory to watch
17
+ VALUE callback; // Proc object to call when there are changes
18
+ BOOL watch_childeren; // Watch sub-directories
19
+ DWORD flags; // Flags for the type of changes to report
20
+ } WDM_EntryUserData, *WDM_PEntryUserData;
21
+
22
+ typedef struct WDM_Entry {
23
+ WDM_PEntryUserData user_data; // User-supplied data
24
+ HANDLE dir_handle; // IO handle of the directory
25
+ BYTE buffer[WDM_BUFFER_SIZE]; // Buffer for the results
26
+ OVERLAPPED event_container; // Async IO event container
27
+ struct WDM_Entry* next; // Well, this is a linked list, so this is self-explanatory :)
28
+ } WDM_Entry, *WDM_PEntry;
29
+
30
+ // ---------------------------------------------------------
31
+ // Prototypes
32
+ // ---------------------------------------------------------
33
+
34
+ WDM_PEntryUserData wdm_entry_user_data_new();
35
+ void wdm_entry_user_data_free(WDM_PEntryUserData);
36
+
37
+ WDM_PEntry wdm_entry_new();
38
+ void wdm_entry_free(WDM_PEntry);
39
+ void wdm_entry_list_free(WDM_PEntry);
40
+
41
+ // ---------------------------------------------------------
42
+
43
+ #ifdef __cplusplus
44
+ }
45
+ #endif // __cplusplus
46
+
47
47
  #endif // WDM_ENTRY_H
@@ -1,27 +1,28 @@
1
- require 'mkmf'
2
- require 'rbconfig'
3
-
4
- def generate_makefile
5
- create_makefile("wdm_ext")
6
- end
7
-
8
- def generate_dummy_makefile
9
- File.open("Makefile", "w") do |f|
10
- f.puts dummy_makefile('wdm_ext').join
11
- end
12
- end
13
-
14
- def windows?
15
- RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/
16
- end
17
-
18
- if windows? and
19
- have_library("kernel32") and
20
- have_header("windows.h") and
21
- have_header("ruby.h") and
22
- have_const('HAVE_RUBY_ENCODING_H')
23
- then
24
- generate_makefile()
25
- else
26
- generate_dummy_makefile()
27
- end
1
+ require 'mkmf'
2
+ require 'rbconfig'
3
+
4
+ def generate_makefile
5
+ create_makefile("wdm_ext")
6
+ end
7
+
8
+ def generate_dummy_makefile
9
+ File.open("Makefile", "w") do |f|
10
+ f.puts dummy_makefile('wdm_ext').join
11
+ end
12
+ end
13
+
14
+ def windows?
15
+ RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/
16
+ end
17
+
18
+ if windows? and
19
+ have_library("kernel32") and
20
+ have_header("windows.h") and
21
+ have_header("ruby.h") and
22
+ have_const('HAVE_RUBY_ENCODING_H')
23
+ then
24
+ have_func('rb_thread_call_without_gvl')
25
+ generate_makefile()
26
+ else
27
+ generate_dummy_makefile()
28
+ end
@@ -1,27 +1,27 @@
1
- #include "wdm.h"
2
-
3
- #include "memory.h"
4
-
5
- void*
6
- wdm_memory_malloc (size_t size)
7
- {
8
- void *memory = malloc(size);
9
-
10
- if ( memory == NULL ) {
11
- rb_fatal("failed to allocate memory");
12
- }
13
-
14
- return memory;
15
- }
16
-
17
- void*
18
- wdm_memory_realloc (void *ptr, size_t size)
19
- {
20
- void *memory = realloc(ptr, size);
21
-
22
- if ( memory == NULL ) {
23
- rb_fatal("failed to re-allocate memory");
24
- }
25
-
26
- return memory;
1
+ #include "wdm.h"
2
+
3
+ #include "memory.h"
4
+
5
+ void*
6
+ wdm_memory_malloc (size_t size)
7
+ {
8
+ void *memory = malloc(size);
9
+
10
+ if ( memory == NULL ) {
11
+ rb_fatal("failed to allocate memory");
12
+ }
13
+
14
+ return memory;
15
+ }
16
+
17
+ void*
18
+ wdm_memory_realloc (void *ptr, size_t size)
19
+ {
20
+ void *memory = realloc(ptr, size);
21
+
22
+ if ( memory == NULL ) {
23
+ rb_fatal("failed to re-allocate memory");
24
+ }
25
+
26
+ return memory;
27
27
  }
@@ -1,30 +1,30 @@
1
- #ifndef WDM_MEMORY_H
2
- #define WDM_MEMORY_H
3
-
4
- #ifdef __cplusplus
5
- extern "C" {
6
- #endif // __cplusplus
7
-
8
- // ---------------------------------------------------------
9
- // Prototypes
10
- // ---------------------------------------------------------
11
-
12
- void* wdm_memory_malloc (size_t);
13
-
14
- void* wdm_memory_realloc (void*, size_t);
15
-
16
- // ---------------------------------------------------------
17
- // Macros
18
- // ---------------------------------------------------------
19
-
20
- #define WDM_ALLOC_N(type,n) ((type*)wdm_memory_malloc((n) * sizeof(type)))
21
- #define WDM_ALLOC(type) ((type*)wdm_memory_malloc(sizeof(type)))
22
- #define WDM_REALLOC_N(var,type,n) ((var)=(type*)wdm_memory_realloc((void*)(var), (n) * sizeof(type)))
23
-
24
- // ---------------------------------------------------------
25
-
26
- #ifdef __cplusplus
27
- }
28
- #endif // __cplusplus
29
-
1
+ #ifndef WDM_MEMORY_H
2
+ #define WDM_MEMORY_H
3
+
4
+ #ifdef __cplusplus
5
+ extern "C" {
6
+ #endif // __cplusplus
7
+
8
+ // ---------------------------------------------------------
9
+ // Prototypes
10
+ // ---------------------------------------------------------
11
+
12
+ void* wdm_memory_malloc (size_t);
13
+
14
+ void* wdm_memory_realloc (void*, size_t);
15
+
16
+ // ---------------------------------------------------------
17
+ // Macros
18
+ // ---------------------------------------------------------
19
+
20
+ #define WDM_ALLOC_N(type,n) ((type*)wdm_memory_malloc((n) * sizeof(type)))
21
+ #define WDM_ALLOC(type) ((type*)wdm_memory_malloc(sizeof(type)))
22
+ #define WDM_REALLOC_N(var,type,n) ((var)=(type*)wdm_memory_realloc((void*)(var), (n) * sizeof(type)))
23
+
24
+ // ---------------------------------------------------------
25
+
26
+ #ifdef __cplusplus
27
+ }
28
+ #endif // __cplusplus
29
+
30
30
  #endif // WDM_MEMORY_H
@@ -1,75 +1,75 @@
1
- #include "wdm.h"
2
-
3
- #include "memory.h"
4
- #include "entry.h"
5
- #include "queue.h"
6
-
7
- #include "monitor.h"
8
-
9
- WDM_PMonitor
10
- wdm_monitor_new()
11
- {
12
- WDM_PMonitor monitor;
13
-
14
- monitor = WDM_ALLOC(WDM_Monitor);
15
-
16
- monitor->running = FALSE;
17
-
18
- monitor->head = NULL;
19
- monitor->monitoring_thread = INVALID_HANDLE_VALUE;
20
-
21
- monitor->changes = wdm_queue_new();
22
-
23
- monitor->process_event = CreateEvent(NULL, TRUE, FALSE, NULL);
24
- monitor->stop_event = CreateEvent(NULL, TRUE, FALSE, NULL);
25
-
26
- if ( ! InitializeCriticalSectionAndSpinCount(&monitor->lock,
27
- 0x00000400) ) // TODO: look into the best value for spinning.
28
- {
29
- rb_raise(eWDM_Error, "Can't create a lock for the monitor");
30
- }
31
-
32
- return monitor;
33
- }
34
-
35
- void
36
- wdm_monitor_free(WDM_PMonitor monitor)
37
- {
38
- if ( monitor->monitoring_thread != INVALID_HANDLE_VALUE ) CloseHandle(monitor->monitoring_thread);
39
-
40
- wdm_entry_list_free(monitor->head);
41
- wdm_queue_free(monitor->changes);
42
- DeleteCriticalSection(&monitor->lock);
43
- CloseHandle(monitor->process_event); // TODO: Look into why this crashes the app when exiting!
44
- CloseHandle(monitor->stop_event);
45
-
46
- free(monitor);
47
- }
48
-
49
- void
50
- wdm_monitor_update_head(WDM_PMonitor monitor, WDM_PEntry new_head)
51
- {
52
- EnterCriticalSection(&monitor->lock);
53
- new_head->next = monitor->head;
54
- monitor->head = new_head;
55
- LeaveCriticalSection(&monitor->lock);
56
- }
57
-
58
- WDM_PMonitorCallbackParam
59
- wdm_monitor_callback_param_new(WDM_PMonitor monitor, WDM_PEntry entry)
60
- {
61
- WDM_PMonitorCallbackParam param;
62
-
63
- param = WDM_ALLOC(WDM_MonitorCallbackParam);
64
-
65
- param->monitor = monitor;
66
- param->entry = entry;
67
-
68
- return param;
69
- }
70
-
71
- void
72
- wdm_monitor_callback_param_free(WDM_PMonitorCallbackParam param)
73
- {
74
- free(param);
1
+ #include "wdm.h"
2
+
3
+ #include "memory.h"
4
+ #include "entry.h"
5
+ #include "queue.h"
6
+
7
+ #include "monitor.h"
8
+
9
+ WDM_PMonitor
10
+ wdm_monitor_new()
11
+ {
12
+ WDM_PMonitor monitor;
13
+
14
+ monitor = WDM_ALLOC(WDM_Monitor);
15
+
16
+ monitor->running = FALSE;
17
+
18
+ monitor->head = NULL;
19
+ monitor->monitoring_thread = INVALID_HANDLE_VALUE;
20
+
21
+ monitor->changes = wdm_queue_new();
22
+
23
+ monitor->process_event = CreateEvent(NULL, TRUE, FALSE, NULL);
24
+ monitor->stop_event = CreateEvent(NULL, TRUE, FALSE, NULL);
25
+
26
+ if ( ! InitializeCriticalSectionAndSpinCount(&monitor->lock,
27
+ 0x00000400) ) // TODO: look into the best value for spinning.
28
+ {
29
+ rb_raise(eWDM_Error, "Can't create a lock for the monitor");
30
+ }
31
+
32
+ return monitor;
33
+ }
34
+
35
+ void
36
+ wdm_monitor_free(WDM_PMonitor monitor)
37
+ {
38
+ if ( monitor->monitoring_thread != INVALID_HANDLE_VALUE ) CloseHandle(monitor->monitoring_thread);
39
+
40
+ wdm_entry_list_free(monitor->head);
41
+ wdm_queue_free(monitor->changes);
42
+ DeleteCriticalSection(&monitor->lock);
43
+ CloseHandle(monitor->process_event); // TODO: Look into why this crashes the app when exiting!
44
+ CloseHandle(monitor->stop_event);
45
+
46
+ free(monitor);
47
+ }
48
+
49
+ void
50
+ wdm_monitor_update_head(WDM_PMonitor monitor, WDM_PEntry new_head)
51
+ {
52
+ EnterCriticalSection(&monitor->lock);
53
+ new_head->next = monitor->head;
54
+ monitor->head = new_head;
55
+ LeaveCriticalSection(&monitor->lock);
56
+ }
57
+
58
+ WDM_PMonitorCallbackParam
59
+ wdm_monitor_callback_param_new(WDM_PMonitor monitor, WDM_PEntry entry)
60
+ {
61
+ WDM_PMonitorCallbackParam param;
62
+
63
+ param = WDM_ALLOC(WDM_MonitorCallbackParam);
64
+
65
+ param->monitor = monitor;
66
+ param->entry = entry;
67
+
68
+ return param;
69
+ }
70
+
71
+ void
72
+ wdm_monitor_callback_param_free(WDM_PMonitorCallbackParam param)
73
+ {
74
+ free(param);
75
75
  }