shmem.cc 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * Copyright [2021] JD.com, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <stddef.h>
  17. #include <stdio.h>
  18. #include <unistd.h>
  19. #include <sys/ipc.h>
  20. #include <sys/shm.h>
  21. #include <sys/mman.h>
  22. #include "shmem.h"
  23. #include "lock/system_lock.h"
  24. SharedMemory::SharedMemory()
  25. : m_key(0), m_id(0), m_size(0), m_ptr(NULL), lockfd(-1)
  26. {
  27. }
  28. SharedMemory::~SharedMemory()
  29. {
  30. mem_detach();
  31. mem_unlock();
  32. }
  33. unsigned long SharedMemory::mem_open(int key)
  34. {
  35. if (m_ptr)
  36. mem_detach();
  37. if (key == 0)
  38. return 0;
  39. m_key = key;
  40. if ((m_id = shmget(m_key, 0, 0)) == -1)
  41. return 0;
  42. struct shmid_ds ds;
  43. if (shmctl(m_id, IPC_STAT, &ds) < 0)
  44. return 0;
  45. return m_size = ds.shm_segsz;
  46. }
  47. unsigned long SharedMemory::mem_create(int key, unsigned long size)
  48. {
  49. if (m_ptr)
  50. mem_detach();
  51. m_key = key;
  52. if (m_key == 0) {
  53. m_ptr = mmap(NULL, size, PROT_READ | PROT_WRITE,
  54. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  55. if (m_ptr != MAP_FAILED)
  56. m_size = size;
  57. else
  58. m_ptr = NULL;
  59. } else {
  60. if ((m_id = shmget(m_key, size,
  61. IPC_CREAT | IPC_EXCL | IPC_PERM)) == -1)
  62. return 0;
  63. struct shmid_ds ds;
  64. if (shmctl(m_id, IPC_STAT, &ds) < 0)
  65. return 0;
  66. m_size = ds.shm_segsz;
  67. }
  68. return m_size;
  69. }
  70. void *SharedMemory::mem_attach(int ro)
  71. {
  72. if (m_ptr)
  73. return m_ptr;
  74. m_ptr = shmat(m_id, NULL, ro ? SHM_RDONLY : 0);
  75. if (m_ptr == MAP_FAILED)
  76. m_ptr = NULL;
  77. return m_ptr;
  78. }
  79. void SharedMemory::mem_detach(void)
  80. {
  81. if (m_ptr) {
  82. if (m_key == 0)
  83. munmap(m_ptr, m_size);
  84. else
  85. shmdt(m_ptr);
  86. }
  87. m_ptr = NULL;
  88. }
  89. int SharedMemory::mem_lock(void)
  90. {
  91. if (lockfd > 0 || m_key == 0)
  92. return 0;
  93. lockfd = unix_socket_lock("tlock-shm-%u", m_key);
  94. if (lockfd < 0)
  95. return -1;
  96. return 0;
  97. }
  98. void SharedMemory::mem_unlock(void)
  99. {
  100. if (lockfd > 0) {
  101. close(lockfd);
  102. lockfd = -1;
  103. }
  104. }
  105. int SharedMemory::mem_delete(void)
  106. {
  107. mem_detach();
  108. if (shmctl(m_id, IPC_RMID, NULL) < 0)
  109. return -1;
  110. return 0;
  111. }