Skip to content
Newer
Older
100644 80 lines (71 sloc) 2.33 KB
fecb101 added native murmur and fnv hashing.
Cliff Moon authored
1
2 #include "murmur.h"
3 #include <erl_driver.h>
4 #include <ei.h>
5 #include <stdio.h>
6
7
8 static ErlDrvData init(ErlDrvPort port, char *cmd);
9 static void stop(ErlDrvData handle);
10 static void outputv(ErlDrvData handle, ErlIOVec *ev);
11 static void send_hash(ErlDrvPort port, unsigned long hash);
12
13 static ErlDrvData init(ErlDrvPort port, char *cmd) {
14 return (ErlDrvData) port;
15 }
16
17 static void stop(ErlDrvData handle) {
18 //noop
19 }
20
21 static void outputv(ErlDrvData handle, ErlIOVec *ev) {
22 ErlDrvPort port = (ErlDrvPort) handle;
23 SysIOVec *bin;
24 int i, n, index = 0;
25 unsigned long hash;
26 unsigned long seed;
27 //first piece of the iovec is the seed
28 // printf("ev->size %d\n", ev->size);
29 // printf("ev-vsize %d\n", ev->vsize);
30 //apparently we start counting at 1 round here?
31 bin = &ev->iov[1];
32 // printf("bin->orig_size %d\n", bin->iov_len);
33 // printf("bin->iov_base %s\n", bin->iov_base);
34 ei_decode_version(bin->iov_base, &index, NULL);
35 ei_decode_ulong(bin->iov_base, &index, &seed);
36 hash = (unsigned int) seed;
37 if (index < bin->iov_len) {
38 hash = MurmurHash2(&bin->iov_base[index], bin->iov_len - index, hash);
39 }
40 // printf("hash %d\n", hash);
41 for (i=2; i<ev->vsize; i++) {
42 bin = &ev->iov[i];
43 // printf("bin->orig_size %d\n", bin->iov_len);
44 hash = MurmurHash2(bin->iov_base, bin->iov_len, hash);
45 // printf("hashed %d\n", i);
46 }
47 send_hash(port, hash);
48 }
49
50 static void send_hash(ErlDrvPort port, unsigned long hash) {
51 ei_x_buff x;
52 ei_x_new_with_version(&x);
53 ei_x_encode_ulong(&x, hash);
54 driver_output(port, x.buff, x.index);
55 // printf("sent hash %d\n", hash);
56 ei_x_free(&x);
57 }
58
59 static ErlDrvEntry murmur_driver_entry = {
60 NULL, /* init */
61 init,
62 stop,
63 NULL, /* output */
64 NULL, /* ready_input */
65 NULL, /* ready_output */
66 "murmur_drv", /* the name of the driver */
67 NULL, /* finish */
68 NULL, /* handle */
69 NULL, /* control */
70 NULL, /* timeout */
71 outputv, /* outputv */
72 NULL, /* ready_async */
73 NULL, /* flush */
74 NULL, /* call */
75 NULL /* event */
76 };
77
78 DRIVER_INIT(murmur_driver) {
79 return &murmur_driver_entry;
80 }
Something went wrong with that request. Please try again.