/
lsTinyCache.php
1093 lines (916 loc) · 29.6 KB
/
lsTinyCache.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/*****************************************************************************
* Copyright (C) 2015 Barry Robertson
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
******************************************************************************
**
** Filename: lsTinyCache.php
** Project: EcoSphere
** Author: Barry Robertson (barry@logixstorm.com)
** Created: 01-16-2015
**
** Abstract: This is a simple little web cache we use for storing
** any persistent data we want to access from the EcoSphere web server.
** This is NOT designed for high-performance production server deployment!!
** We'll use AWS ElastiCache or some other web cache implementation and
** almost assuredly not use these little PHP modules for the real heavy
** lifting in the true, production implementation. But PHP is so simple
** and just molds like silly putty in a matter of minutes! ha!
** ...go f--- yourself if you don't agree! have a nice day
**
** The way this works is by using php's shared memory operations. They
** don't work to talk from Apache to some other process (sadly...) but
** they *do* persist across client access on the server!
**
** The basic design is a page-based linear memory allocator that stores
** a catalog memory segment in one memory key which is read and written
** entirely each time a cache entry is added or removed from the system.
**
** In a separate file, we store the data associated with the cache keys.
** This is just a raw, offset/length sort of organization and is split
** up into 1k 'pages' controlled by the catalog object referenced above.
**
** Instancing a cache is simple:
**
** $cache = LsTinyCache::Factory( $key, $size );
** if( $cache === null )
** {
** if( $size == 0 )
** {
** return(ENOENT);
** }
** else
** {
** return( EFAULT );
** }
** }
**
**
** Change Log:
** Barry Robertson; 01-16-2015; Initial version.
** <engr. name>; <date>; <reason>
*****************************************************************************/
//**************************************************************************
//* Use Modules
//**************************************************************************
//**************************************************************************
//* Include/Require Files
//**************************************************************************
require_once( "lsUtil.php" );
require_once( "lsErrno.php" );
//**************************************************************************
//* Name Space
//**************************************************************************
//**************************************************************************
//* Public Functions
//**************************************************************************
//**************************************************************************
//************************ Classes *****************************************
//**************************************************************************
class LsCacheEntry
{
public $key = -1;
public $dcacheOffset = -1;
public $pageCount = 0; // Total length of the original allocation.
public $valueLen=0; // current length of the string stored in the segment.
}
/**
* @abstract This is the catalog object used to control what memory segments
* are available in the data file.
*/
class LsTinyCache
{
//*************************************************************************
//*************************************************************************
// Private Members
//*************************************************************************
//*************************************************************************
public $cacheBitmap = null;
public $cacheSize = 0;
public $toc = array();
private $pageSize = LsTinyCache::PAGE_SIZE;
//
// These aren't stored as part of the in-memory catalog
//
private $numPages = 0;
private $lockFileName = "";
private $lockFd = -1;
//
// Shared memory resources
//
private $catalogShmKey = -1;
private $catalogShmHandle = -1;
private $cacheShmKey = -1;
private $cacheShmHandle = -1;
//
// If this flag is set, then we were simply passed a key for the cache
// and we're going to discover how big it is and allocate the shared
// memory objects accordingly.
//
private $discoveryMode = LsTinyCache::MANUAL;
//
// This is the current size of the serialized catalog.
// (Gets updated everytime the catalog gets written to shmem...)
//
private $catalogSerSize = 0;
//
// This is the current size of the allocated catalog memory segments.
//
private $catalogMemSize = 0;
//*************************************************************************
//*************************************************************************
// Protected Members
//*************************************************************************
//*************************************************************************
//*************************************************************************
//*************************************************************************
// Public Members
//*************************************************************************
//*************************************************************************
//*************************************************************************
//*************************************************************************
// Constants
//*************************************************************************
//*************************************************************************
const PAGE_SIZE = 1024;
const MAGIC = 'FEEDBEEF';
const CATALOG_MIN_SIZE = 32768;
const ZERO_BLK_SIZE = 65536;
const HEADER_SIZE = 16;
const CATALOG_OFFSET = 16;
const MAGIC_OFFSET = 0;
const CATALOG_SIZE_OFFSET = 8;
const MANUAL = 'manual';
const AUTO = 'auto';
//*************************************************************************
//*************************************************************************
// Static Public Methods
//*************************************************************************
//*************************************************************************
//*************************************************************************
//*************************************************************************
// Constructor & Abstract (Virtual) Methods
//*************************************************************************
//*************************************************************************
public function __construct( $keyId, $cacheSize=0 )
{
//
// This means we're just instancing the basics to communicate with
// the lock file and the catalog. The actual size of the objects
// will be discovered.
//
$this->pageSize = LsTinyCache::PAGE_SIZE;
if( $cacheSize == 0 )
{
$this->discoveryMode = LsTinyCache::AUTO;
$this->cacheSize = 0;
$this->numPages = 0;
}
else
{
$this->cacheSize = LsRoundUp( $cacheSize, $this->pageSize );
$this->numPages = $this->cacheSize / $this->pageSize;
}
$this->catalogShmKey = $keyId;
$this->cacheShmKey = $keyId + 4096;
$this->lockFileName = LsSys::GetEcosphereIpcLockFile($keyId);
}
//*************************************************************************
//*************************************************************************
// Public Methods
//*************************************************************************
//*************************************************************************
public static function Factory( $keyId, $cacheSize=0 )
{
$cache = new LsTinyCache( $keyId, $cacheSize );
//
// Open the lock file.
//
$err = $cache->OpenLockFile();
if( $err != EOK )
{
return( $err );
}
//
// See if it's in memory already.
//
$err = $cache->LoadFromMemory();
if( $err == EOK )
{
return( $cache );
}
//***************************************************************
// See if we get ENOENT && we're in auto-discovery mode.
// In this case, we can't actually do this..
//***************************************************************
if( $err==ENOENT )
{
if( $cache->GetDiscoveryMode() == LsTinyCache::AUTO )
{
return( null );
}
$err = $cache->WriteLock();
if( $err != EOK )
{
return( null );
}
//
// We can create a new cache object.
//
$err = $cache->AllocateInternalResources();
if( $err != EOK )
{
$cache->Unlock();
return( null );
}
$err = $cache->WriteCatalog();
if( $err != EOK )
{
$cache->Unlock();
return( null );
}
$cache->Unlock();
return( $cache );
}
else
{
return( null );
}
}
public function GetSizeOfDataCache(){ return( $this->numPages * $this->pageSize ); }
public function GetPageSize() { return( $this->pageSize ); }
public function OffsetToPage( $offset ){ return( $offset / $this->pageSize ); }
public function PageToOffset( $page ){ return( $page * $this->pageSize ); }
public function ClearTheCache( $bZeroData=false, $bLockHeld=false )
{
if( $bLockHeld == false )
{
$err = $this->WriteLock();
if( $err != EOK )
{
return( $err );
}
}
$this->toc = array();
$this->cacheBitmap->Reset();
//
// Update the catalog.
//
$err = $this->WriteCatalog();
if( $err == EOK && $bZeroData == true )
{
$err = $this->ZeroCacheData();
}
if( $bLockHeld == false ){ $this->Unlock(); }
return( $err );
}
public function AllocateKey( $key, $maxSize )
{
$err = $this->WriteLock();
if( $err != EOK )
{
return( $err );
}
if( isset($this->toc[$key]) )
{
//
// Make sure the size is ok if it already exists.
//
if( $maxSize != ($this->toc[$key]->pageCount * $this->pageSize) )
{
$this->Unlock();
return( ENOSPC );
}
$this->Unlock();
return( EOK );
}
$entry = $this->AllocateCacheEntry( $key, $maxSize );
if( $entry == null )
{
$this->Unlock();
return( ENOMEM );
}
//
// Add the key to the catalog.
//
$this->toc[$key] = $entry;
//
// Update the catalog.
//
$err = $this->WriteCatalog();
$this->Unlock();
return( $err );
}
public function PutKey( $key, $value )
{
$err = $this->WriteLock();
if( $err != EOK )
{
return( $err );
}
$len = strlen($value);
if( isset($this->toc[$key]) )
{
//
// Make sure the size is ok.
//
$entry = $this->toc[$key];
if( $len > ($entry->pageCount * $this->pageSize) )
{
$this->Unlock();
return( ENOSPC );
}
}
else
{
//
// Try to get a catalog entry for this size.
//
$entry = $this->AllocateCacheEntry( $key, $len );
if( $entry == null )
{
$this->Unlock();
return( ENOMEM );
}
//
// Add the key to the catalog.
//
$this->toc[$key] = $entry;
}
//
// Update the current value in the cache memory block.
// Write the cache entry.
//
$entry->valueLen = $len;
$err = $this->WriteCacheEntry( $entry, $value );
if( $err != EOK )
{
$this->Unlock();
return( $err );
}
//
// Update the catalog.
//
$err = $this->WriteCatalog();
$this->Unlock();
return( $err );
}
public function ReadKeyValue( $key, &$data )
{
$err = $this->ReadLock();
if( $err != EOK )
{
return( $err );
}
if( !isset($this->toc[$key]) )
{
$this->Unlock();
return( ENOENT );
}
//
// Read the data segments for this cache entry.
//
$ce = $this->toc[$key];
$data = shmop_read( $this->cacheShmHandle, $ce->dcacheOffset, $ce->valueLen );
if( $data === false )
{
$this->Unlock();
return( EIO );
}
$this->Unlock();
return( EOK );
}
public function DeleteCache()
{
//
// If the thing hasn't been initialized, we can't say the cache has been 'deleted'.
//
if( $this->lockFd == -1 )
{
return( EPERM );
}
$this->WriteLock();
//
// Delete all the data.
// (true=zeroData, true=lockHeld)
//
$err = $this->ClearTheCache( true, true );
if( $err != EOK )
{
$this->Unlock();
return( $err );
}
//
// Blow away the header so it won't be confused as an existing cache.
//
if( $this->catalogShmHandle != -1 )
{
$catalogBytesToWrite = LsRoundUp(
$this->catalogSerSize, LsTinyCache::CATALOG_MIN_SIZE );
$result = shmop_write(
$this->catalogShmHandle,
str_repeat(' ', LsTinyCache::HEADER_SIZE + $catalogBytesToWrite),
0 ); // Offset into the memory.
if( $result === false )
{
$this->Unlock();
return( EIO );
}
}
//
// Delete the catalog and data cache objects.
//
LsTinyCache::DeleteShmObject( $this->catalogShmHandle );
LsTinyCache::DeleteShmObject( $this->cacheShmHandle );
//
// (true=lock held)
//
$this->Close( true );
//
// Close out the cache.
//
$this->Unlock();
return( EOK );
}
public function Close( $bLockHeld=false )
{
if( $bLockHeld == false )
{
$this->WriteLock();
}
if( $this->cacheShmHandle != -1 )
{
shmop_close( $this->cacheShmHandle );
$this->cacheShmHandle = -1;
}
if( $this->catalogShmHandle != -1 )
{
shmop_close( $this->catalogShmHandle );
$this->catalogShmHandle = -1;
}
if( $bLockHeld == false )
{
$this->Unlock();
}
fclose( $this->lockFd );
$this->lockFd = -1;
}
//*************************************************************************
//*************************************************************************
// Protected Methods
//*************************************************************************
//*************************************************************************
//*************************************************************************
//*************************************************************************
// Private Methods
//*************************************************************************
//*************************************************************************
private function GetDiscoveryMode()
{
return( $this->discoveryMode );
}
private function OpenLockFile( )
{
$lockFile = $this->lockFileName;
if( $this->lockFd != -1 )
{
return( EPERM );
}
//
// Open the lockfile as w+ and immediately close it and reopen as 'r'.
//
$fd = fopen( $lockFile, 'w+' );
if( $fd === false )
{
return( EIO );
}
//
// Close it and reopen and read-only.
//
fclose( $fd );
$this->lockFd = fopen( $lockFile, 'r' );
if( $this->lockFd === false )
{
$this->lockFd = -1;
return( EIO );
}
return( EOK );
}
private function AllocateInternalResources()
{
if( $this->numPages == 0 ||
$this->cacheShmHandle != -1 ||
$this->catalogShmHandle != -1 )
{
return( EPERM );
}
//
// Create the bitmap to control the page allocations.
//
$this->cacheBitmap = new LsBitmap( $this->numPages );
//
// Create the shared memory objects.
//
$err = $this->CreateSharedMemoryRegions();
if( $err != EOK )
{
return( $err );
}
return( EOK );
}
//
// Catalog must be write locked.
//
private function WriteCatalog( )
{
if( $this->catalogShmHandle == -1 )
{
return( EPERM );
}
$s = serialize( $this );
$this->catalogSerSize = strlen( $s );
$data = LsTinyCache::CreateCatalogHeader($this->catalogSerSize) . $s;
//
// Read the first 16 bytes.
//
$result = shmop_write( $this->catalogShmHandle, $data, 0 );
if( $result === false )
{
return( EIO );
}
return( EOK );
}
private function LoadFromMemory()
{
$err = $this->WriteLock();
if( $err != EOK )
{
return( $err );
}
//
// Check to see if this has already been done.
//
if( $this->catalogShmHandle != -1 || $this->cacheShmHandle != -1)
{
$this->Unlock();
return( EPERM );
}
$catalog = LsTinyCache::OpenCatalogFromShm( $this->catalogShmKey, $shmHandle );
if( $catalog === null )
{
//
// Doesn't exist in memory.
//
$this->Unlock();
return( ENOENT );
}
//**********************************************************************
// Check to make sure that we're in auto-discovery mode.
// If we have a valid catalog onfile, we have to verify that they're
// the same size...
//**********************************************************************
if( $this->discoveryMode != LsTinyCache::AUTO )
{
if( $this->GetSizeOfDataCache() != $catalog->GetSizeOfDataCache() ||
$this->pageSize != $catalog->GetPageSize() )
{
$this->Unlock();
return( ESIZEMISMATCH );
}
}
//**********************************************************************
// Set the shared memory handle used to read in the stored catalog.
//**********************************************************************
$this->catalogShmHandle = $shmHandle;
$this->cacheSize = $catalog->cacheSize;
$this->pageSize = $catalog->GetPageSize();
$this->numPages = $this->cacheSize / $this->pageSize;
$this->cacheBitmap = clone $catalog->cacheBitmap;
$this->toc = $catalog->toc;
//
// Now open up the data cache resources.
//
$this->cacheShmHandle = LsTinyCache::CreateShmObject(
$this->cacheShmKey, $this->numPages*$this->pageSize );
if( $this->cacheShmHandle === -1 )
{
$this->Unlock();
return( EIO );
}
$this->Unlock();
return( EOK );
}
//
// Catalog must be write locked.
//
private function WriteCacheEntry( &$ce, $data )
{
if( $this->cacheShmHandle == -1 )
{
return( EPERM );
}
$result = shmop_write( $this->cacheShmHandle, $data, $ce->dcacheOffset );
if( $result === false )
{
return( EIO );
}
return( EOK );
}
//
// Object must be write locked.
//
private function AllocateCacheEntry( $cacheKey, $size )
{
$cacheLen = LsRoundUp( $size, $this->pageSize );
if( $cacheLen > $this->cacheSize )
{
return( null );
}
$pageCount = $cacheLen / $this->pageSize;
//
// Get a range of pages that are clear.
//
$startBit = $this->cacheBitmap->GetRange( $pageCount );
if( $startBit == -1 )
{
return( null );
}
$ce = new LsCacheEntry;
$ce->key = $cacheKey;
$ce->dcacheOffset = $this->PageToOffset($startBit);
$ce->pageCount = $pageCount;
$ce->valueLen = 0;
return( $ce );
}
private function CreateSharedMemoryRegions( )
{
if( $this->catalogShmHandle != -1 ||
$this->cacheShmHandle != -1 )
{
return( EPERM );
}
//
// Create the catalog shm file.
//
$this->catalogShmHandle = LsTinyCache::CreateShmObject(
$this->catalogShmKey,
LsTinyCache::HEADER_SIZE + LsTinyCache::CATALOG_MIN_SIZE );
//*********************************************************************
// Create the cache memory shm file.
//*********************************************************************
$this->cacheShmHandle = LsTinyCache::CreateShmObject(
$this->cacheShmKey,
$this->numPages*$this->pageSize );
return( EOK );
}
//
// Write/read lock must be held.
//
private static function ReadCatalogHeader( $shmHandle, &$magic, &$size )
{
if( $shmHandle == -1 )
{
return( EPERM );
}
//
// Read the first 16 bytes.
//
$header = shmop_read( $shmHandle, 0, LsTinyCache::HEADER_SIZE );
if( $header === false )
{
return( EIO );
}
$magic = substr( $header, 0, 8 );
$size = intval( hexdec( substr( $header, 8, 8 ) ) );
return( EOK );
}
private static function CreateCatalogHeader( $size )
{
$sizeStr = sprintf( "%08X", $size );
return( LsTinyCache::MAGIC . $sizeStr );
}
private static function OpenCatalogSharedMemory( $key, $size )
{
//
// Round up the size to our minimum size.
//
$size = LsRoundUp( $size, LsTinyCache::CATALOG_MIN_SIZE );
return( LsTinyCache::CreateShmObject($key,$size+LsTinyCache::HEADER_SIZE) );
}
//
// Write lock must be held
//
private static function OpenCatalogFromShm( $key, &$returnedFileHandle )
{
$returnedFileHandle = null;
//
// Open the shared memory object for just the header.
//
$headerHandle = LsTinyCache::CreateShmObject( $key, LsTinyCache::CATALOG_MIN_SIZE );
if( $headerHandle === -1 )
{
//
// This object doesn't exist...
//
return( null );
}
//
// Read the header.
//
$err = LsTinyCache::ReadCatalogHeader( $headerHandle, $magic, $sizeOfSerializedCatalog );
if( $err != EOK )
{
shmop_close( $headerHandle );
return( null );
}
//
// See if the object exists in shared memory.
//
if( $magic != LsTinyCache::MAGIC )
{
//
// The object doesn't exist!
//
shmop_close( $headerHandle );
return( null );
}
//
// Check to see if the catalog is larger than one, base size block.
// If it's currently smaller than the base unit, then we don't need
// to reopen it.
//
if( $sizeOfSerializedCatalog > LsTinyCache::CATALOG_MIN_SIZE )
{
//
// Close the shm handle and reopen at the correct size.
//
shmop_close( $headerHandle );
//********************************************************************
// Now create the catalog file at the correct size.
//********************************************************************
$returnedFileHandle = LsTinyCache::OpenCatalogSharedMemory(
$key, $sizeOfSerializedCatalog );
if( $returnedFileHandle === -1 )
{
return( null );
}
}
else
{
//
// We can just reuse this file descriptor because it's small enough
// to fit into one unit.
//
$returnedFileHandle = $headerHandle;
}
//
// Read in the raw, serialized data and unserialize it.
//
$s = shmop_read( $returnedFileHandle,
LsTinyCache::CATALOG_OFFSET, $sizeOfSerializedCatalog );
if( $s === false )
{
return( null );
}
//
// Now unserialize the catalog and copy off the fields we want.
//
$catalog = unserialize( $s );
if( $catalog === null )
{
return( null );
}
return( $catalog );
}
private static function DeleteShmObject( $handle )
{
if( $handle == -1 )
{
return( EINVAL );
}
$result = shmop_delete( $handle );
if( $result === true )
{
return( EOK );
}
return( EIO );
}
private static function CloseShmObject( $handle )
{
if( $handle == -1 )
{
return( EINVAL );
}
$result = shmop_close( $handle );
if( $result === true )
{
return( EOK );
}
return( EIO );
}
private static function OpenShmObject( $key, $size, $mode='a', $permissions=0666 )
{
$handle = @shmop_open( $key, $mode, $permissions, $size );
if( $handle === false )
{
return( -1 );
}
return( $handle );
}
private static function CreateShmObject( $key, $size, $permissions=0666 )
{
$handle = @shmop_open( $key, 'n', $permissions, $size );
if( $handle === false )
{
//
// Attempt to open it with write permissions if it already exists.
//
$handle = @shmop_open( $key, 'w', $permissions, $size );