scws4r 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/ext/scws4r/lock.c ADDED
@@ -0,0 +1,153 @@
1
+ /*
2
+ +----------------------------------------------------------------------+
3
+ | PHP Version 5 |
4
+ +----------------------------------------------------------------------+
5
+ | Copyright (c) 1997-2007 The PHP Group |
6
+ +----------------------------------------------------------------------+
7
+ | This source file is subject to version 3.01 of the PHP license, |
8
+ | that is bundled with this package in the file LICENSE, and is |
9
+ | available through the world-wide-web at the following url: |
10
+ | http://www.php.net/license/3_01.txt |
11
+ | If you did not receive a copy of the PHP license and are unable to |
12
+ | obtain it through the world-wide-web, please send a note to |
13
+ | license@php.net so we can mail you a copy immediately. |
14
+ +----------------------------------------------------------------------+
15
+ | Author: Sascha Schumann <sascha@schumann.cx> |
16
+ +----------------------------------------------------------------------+
17
+ */
18
+
19
+ /* $Id$ */
20
+
21
+ #ifdef HAVE_CONFIG_H
22
+ # include "config.h"
23
+ #endif
24
+
25
+ #include <errno.h>
26
+ #include "lock.h"
27
+
28
+ #if HAVE_STRUCT_FLOCK
29
+ # include <unistd.h>
30
+ # include <fcntl.h>
31
+ # include <sys/file.h>
32
+ #endif
33
+
34
+ #ifdef WIN32
35
+ # include "config_win32.h"
36
+ # include <winsock.h>
37
+ #endif
38
+
39
+ #ifdef NETWARE
40
+ # include <netinet/in.h>
41
+ #endif
42
+
43
+
44
+ int _xdb_flock(int fd, int operation)
45
+ #if HAVE_STRUCT_FLOCK
46
+ {
47
+ struct flock flck;
48
+ int ret;
49
+
50
+ flck.l_start = flck.l_len = 0;
51
+ flck.l_whence = SEEK_SET;
52
+
53
+ if (operation & LOCK_SH)
54
+ flck.l_type = F_RDLCK;
55
+ else if (operation & LOCK_EX)
56
+ flck.l_type = F_WRLCK;
57
+ else if (operation & LOCK_UN)
58
+ flck.l_type = F_UNLCK;
59
+ else {
60
+ errno = EINVAL;
61
+ return -1;
62
+ }
63
+
64
+ ret = fcntl(fd, operation & LOCK_NB ? F_SETLK : F_SETLKW, &flck);
65
+
66
+ if (operation & LOCK_NB && ret == -1 &&
67
+ (errno == EACCES || errno == EAGAIN))
68
+ errno = EWOULDBLOCK;
69
+
70
+ if (ret != -1) ret = 0;
71
+
72
+ return ret;
73
+ }
74
+ #elif defined(WIN32)
75
+ /*
76
+ * Program: Unix compatibility routines
77
+ *
78
+ * Author: Mark Crispin
79
+ * Networks and Distributed Computing
80
+ * Computing & Communications
81
+ * University of Washington
82
+ * Administration Building, AG-44
83
+ * Seattle, WA 98195
84
+ * Internet: MRC@CAC.Washington.EDU
85
+ *
86
+ * Date: 14 September 1996
87
+ * Last Edited: 14 August 1997
88
+ *
89
+ * Copyright 1997 by the University of Washington
90
+ *
91
+ * Permission to use, copy, modify, and distribute this software and its
92
+ * documentation for any purpose and without fee is hereby granted, provided
93
+ * that the above copyright notice appears in all copies and that both the
94
+ * above copyright notice and this permission notice appear in supporting
95
+ * documentation, and that the name of the University of Washington not be
96
+ * used in advertising or publicity pertaining to distribution of the software
97
+ * without specific, written prior permission. This software is made available
98
+ * "as is", and
99
+ * THE UNIVERSITY OF WASHINGTON DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED,
100
+ * WITH REGARD TO THIS SOFTWARE, INCLUDING WITHOUT LIMITATION ALL IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, AND IN
101
+ * NO EVENT SHALL THE UNIVERSITY OF WASHINGTON BE LIABLE FOR ANY SPECIAL,
102
+ * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
103
+ * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, TORT
104
+ * (INCLUDING NEGLIGENCE) OR STRICT LIABILITY, ARISING OUT OF OR IN CONNECTION
105
+ * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
106
+ *
107
+ */
108
+ /* DEDICATION
109
+
110
+ * This file is dedicated to my dog, Unix, also known as Yun-chan and
111
+ * Unix J. Terwilliker Jehosophat Aloysius Monstrosity Animal Beast. Unix
112
+ * passed away at the age of 11 1/2 on September 14, 1996, 12:18 PM PDT, after
113
+ * a two-month bout with cirrhosis of the liver.
114
+ *
115
+ * He was a dear friend, and I miss him terribly.
116
+ *
117
+ * Lift a leg, Yunie. Luv ya forever!!!!
118
+ */
119
+ {
120
+ HANDLE hdl = (HANDLE) _get_osfhandle(fd);
121
+ DWORD low = 1, high = 0;
122
+ OVERLAPPED offset =
123
+ {0, 0, 0, 0, NULL};
124
+ if (hdl < 0)
125
+ return -1; /* error in file descriptor */
126
+ /* bug for bug compatible with Unix */
127
+ UnlockFileEx(hdl, 0, low, high, &offset);
128
+ switch (operation & ~LOCK_NB) { /* translate to LockFileEx() op */
129
+ case LOCK_EX: /* exclusive */
130
+ if (LockFileEx(hdl, LOCKFILE_EXCLUSIVE_LOCK +
131
+ ((operation & LOCK_NB) ? LOCKFILE_FAIL_IMMEDIATELY : 0),
132
+ 0, low, high, &offset))
133
+ return 0;
134
+ break;
135
+ case LOCK_SH: /* shared */
136
+ if (LockFileEx(hdl, ((operation & LOCK_NB) ? LOCKFILE_FAIL_IMMEDIATELY : 0),
137
+ 0, low, high, &offset))
138
+ return 0;
139
+ break;
140
+ case LOCK_UN: /* unlock */
141
+ return 0; /* always succeeds */
142
+ default: /* default */
143
+ break;
144
+ }
145
+ return -1;
146
+ }
147
+ #else
148
+ #warning no proper flock supported
149
+ {
150
+ errno = 0;
151
+ return 0;
152
+ }
153
+ #endif
data/ext/scws4r/lock.h ADDED
@@ -0,0 +1,44 @@
1
+ /*
2
+ +----------------------------------------------------------------------+
3
+ | PHP Version 5 |
4
+ +----------------------------------------------------------------------+
5
+ | Copyright (c) 1997-2007 The PHP Group |
6
+ +----------------------------------------------------------------------+
7
+ | This source file is subject to version 3.01 of the PHP license, |
8
+ | that is bundled with this package in the file LICENSE, and is |
9
+ | available through the world-wide-web at the following url: |
10
+ | http://www.php.net/license/3_01.txt |
11
+ | If you did not receive a copy of the PHP license and are unable to |
12
+ | obtain it through the world-wide-web, please send a note to |
13
+ | license@php.net so we can mail you a copy immediately. |
14
+ +----------------------------------------------------------------------+
15
+ | Author: Sascha Schumann <sascha@schumann.cx> |
16
+ +----------------------------------------------------------------------+
17
+ */
18
+
19
+ /* $Id$ */
20
+
21
+ #ifndef LOCK_H
22
+ #define LOCK_H
23
+
24
+ /* php_flock internally uses fcntl whether or not flock is available
25
+ * This way our php_flock even works on NFS files.
26
+ * More info: /usr/src/linux/Documentation
27
+ */
28
+ int _xdb_flock(int fd, int operation);
29
+
30
+ #ifndef HAVE_FLOCK
31
+ # define LOCK_SH 1
32
+ # define LOCK_EX 2
33
+ # define LOCK_NB 4
34
+ # define LOCK_UN 8
35
+ #endif
36
+
37
+ #ifdef WIN32
38
+ # define EWOULDBLOCK WSAEWOULDBLOCK
39
+ # define fsync _commit
40
+ # define ftruncate(a, b) chsize(a, b)
41
+ # include <windows.h>
42
+ #endif /* defined(WIN32) */
43
+
44
+ #endif /* LOCK_H */
data/ext/scws4r/pool.c ADDED
@@ -0,0 +1,141 @@
1
+ /**
2
+ * @file pool.c
3
+ * @author Hightman Mar
4
+ * @editor set number ; syntax on ; set autoindent ; set tabstop=4 (vim)
5
+ * $Id$
6
+ */
7
+
8
+ #include "pool.h"
9
+ #include <stdio.h>
10
+ #include <stdlib.h>
11
+ #ifndef WIN32
12
+ # include <unistd.h>
13
+ #endif
14
+ #include <string.h>
15
+
16
+ /** pool memory management */
17
+ static void _pool_append_clean(pool_t p, void *obj)
18
+ {
19
+ struct pclean *c;
20
+
21
+ p->size += sizeof(struct pclean);
22
+ c = (struct pclean *) malloc(sizeof(struct pclean));
23
+ c->obj = obj;
24
+ c->nxt = p->clean;
25
+ p->clean = c;
26
+ }
27
+
28
+ static void _pool_heap_new(pool_t p)
29
+ {
30
+ if (p->heap != NULL)
31
+ p->dirty += (p->heap->size - p->heap->used);
32
+
33
+ p->heap = (struct pheap *) malloc(POOL_BLK_SIZ);
34
+ p->heap->size = POOL_BLK_SIZ - sizeof(struct pheap);
35
+ p->heap->used = 0;
36
+ p->size += POOL_BLK_SIZ;
37
+
38
+ _pool_append_clean(p, (void *) p->heap);
39
+ }
40
+
41
+
42
+ pool_t pool_new()
43
+ {
44
+ pool_t p;
45
+
46
+ p = (pool_t) malloc(sizeof(pool_st));
47
+ p->size = sizeof(pool_st);
48
+ p->dirty = 0;
49
+ p->heap = NULL;
50
+ p->clean = NULL;
51
+ _pool_heap_new(p);
52
+ return p;
53
+ }
54
+
55
+ void pool_free(pool_t p)
56
+ {
57
+ struct pclean *cur, *nxt;
58
+
59
+ cur = p->clean;
60
+ while (cur != NULL)
61
+ {
62
+ free(cur->obj);
63
+ nxt = cur->nxt;
64
+ free(cur);
65
+ cur = nxt;
66
+ }
67
+ free(p);
68
+ }
69
+
70
+ void *pmalloc(pool_t p, int size)
71
+ {
72
+ void *block;
73
+
74
+ /* big request */
75
+ if (size > (p->heap->size / 4))
76
+ {
77
+ block = malloc(size);
78
+ p->size += size;
79
+ _pool_append_clean(p, block);
80
+ return block;
81
+ }
82
+
83
+ /* memory align (>=4) */
84
+ if (size & 0x04)
85
+ {
86
+ while (p->heap->used & 0x03)
87
+ {
88
+ p->dirty++;
89
+ p->heap->used++;
90
+ }
91
+ }
92
+
93
+ /* not enough? */
94
+ if (size > (p->heap->size - p->heap->used))
95
+ _pool_heap_new(p);
96
+
97
+ block = (void *)((char *) p->heap->block + p->heap->used);
98
+ p->heap->used += size;
99
+ return block;
100
+ }
101
+
102
+ void *pmalloc_x(pool_t p, int size, char c)
103
+ {
104
+ void *result = pmalloc(p, size);
105
+ memset(result, c, size);
106
+ return result;
107
+ }
108
+
109
+ void *pmalloc_z(pool_t p, int size)
110
+ {
111
+ return pmalloc_x(p, size, 0);
112
+ }
113
+
114
+ char *pstrdup(pool_t p, const char *src)
115
+ {
116
+ char *dst;
117
+ int len;
118
+
119
+ if (src == NULL)
120
+ return NULL;
121
+
122
+ len = strlen(src) + 1;
123
+ dst = (char *) pmalloc(p, len);
124
+ memcpy(dst, src, len);
125
+ return dst;
126
+ }
127
+
128
+ char *pstrndup(pool_t p, const char *src, int len)
129
+ {
130
+ char *dst;
131
+
132
+ if (src == NULL)
133
+ return NULL;
134
+
135
+ dst = (char *) pmalloc(p, len + 1);
136
+ memcpy(dst, src, len);
137
+ dst[len] = '\0';
138
+
139
+ return dst;
140
+ }
141
+
data/ext/scws4r/pool.h ADDED
@@ -0,0 +1,53 @@
1
+ /**
2
+ * @file pool.h
3
+ * @author Hightman Mar
4
+ * @editor set number ; syntax on ; set autoindent ; set tabstop=4 (vim)
5
+ * $Id$
6
+ */
7
+
8
+ #ifndef _SCWS_POOL_20070525_H_
9
+ #define _SCWS_POOL_20070525_H_
10
+
11
+ #ifdef __cplusplus
12
+ extern "C" {
13
+ #endif
14
+
15
+ /* block size for pool */
16
+ #define POOL_BLK_SIZ 4096
17
+
18
+ /* data structure for pool */
19
+ struct pheap
20
+ {
21
+ int size;
22
+ int used;
23
+ char block[0];
24
+ };
25
+
26
+ struct pclean
27
+ {
28
+ void *obj;
29
+ struct pclean *nxt;
30
+ };
31
+
32
+ typedef struct
33
+ {
34
+ int size; /* total allocated */
35
+ int dirty; /* total wasted */
36
+ struct pheap *heap;
37
+ struct pclean *clean;
38
+ } pool_st, *pool_t;
39
+
40
+ /* pool: api */
41
+ pool_t pool_new(); /* create a new memory pool with an initial heap size */
42
+ void pool_free(pool_t p); /* frees all the data on the pool & delete the pool itself */
43
+ void *pmalloc(pool_t p, int size); /* wrapper around malloc, takes from the pool */
44
+ void *pmalloc_x(pool_t p, int size, char c); /* wrapper around pmalloc which prefills buffer with c */
45
+ void *pmalloc_z(pool_t p, int size); /* wrapper around pmalloc, which prefills by zero */
46
+ char *pstrdup(pool_t p, const char *s); /* wrapper around strdup, gains the mem from the pool */
47
+ char *pstrndup(pool_t p, const char *s, int l);
48
+
49
+ #ifdef __cplusplus
50
+ }
51
+ #endif
52
+
53
+ #endif