posixshmem.c.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*[clinic input]
  2. preserve
  3. [clinic start generated code]*/
  4. #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
  5. # include "pycore_gc.h" // PyGC_Head
  6. # include "pycore_runtime.h" // _Py_ID()
  7. #endif
  8. #if defined(HAVE_SHM_OPEN)
  9. PyDoc_STRVAR(_posixshmem_shm_open__doc__,
  10. "shm_open($module, /, path, flags, mode=511)\n"
  11. "--\n"
  12. "\n"
  13. "Open a shared memory object. Returns a file descriptor (integer).");
  14. #define _POSIXSHMEM_SHM_OPEN_METHODDEF \
  15. {"shm_open", _PyCFunction_CAST(_posixshmem_shm_open), METH_FASTCALL|METH_KEYWORDS, _posixshmem_shm_open__doc__},
  16. static int
  17. _posixshmem_shm_open_impl(PyObject *module, PyObject *path, int flags,
  18. int mode);
  19. static PyObject *
  20. _posixshmem_shm_open(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
  21. {
  22. PyObject *return_value = NULL;
  23. #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
  24. #define NUM_KEYWORDS 3
  25. static struct {
  26. PyGC_Head _this_is_not_used;
  27. PyObject_VAR_HEAD
  28. PyObject *ob_item[NUM_KEYWORDS];
  29. } _kwtuple = {
  30. .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
  31. .ob_item = { &_Py_ID(path), &_Py_ID(flags), &_Py_ID(mode), },
  32. };
  33. #undef NUM_KEYWORDS
  34. #define KWTUPLE (&_kwtuple.ob_base.ob_base)
  35. #else // !Py_BUILD_CORE
  36. # define KWTUPLE NULL
  37. #endif // !Py_BUILD_CORE
  38. static const char * const _keywords[] = {"path", "flags", "mode", NULL};
  39. static _PyArg_Parser _parser = {
  40. .keywords = _keywords,
  41. .fname = "shm_open",
  42. .kwtuple = KWTUPLE,
  43. };
  44. #undef KWTUPLE
  45. PyObject *argsbuf[3];
  46. Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 2;
  47. PyObject *path;
  48. int flags;
  49. int mode = 511;
  50. int _return_value;
  51. args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 3, 0, argsbuf);
  52. if (!args) {
  53. goto exit;
  54. }
  55. if (!PyUnicode_Check(args[0])) {
  56. _PyArg_BadArgument("shm_open", "argument 'path'", "str", args[0]);
  57. goto exit;
  58. }
  59. if (PyUnicode_READY(args[0]) == -1) {
  60. goto exit;
  61. }
  62. path = args[0];
  63. flags = _PyLong_AsInt(args[1]);
  64. if (flags == -1 && PyErr_Occurred()) {
  65. goto exit;
  66. }
  67. if (!noptargs) {
  68. goto skip_optional_pos;
  69. }
  70. mode = _PyLong_AsInt(args[2]);
  71. if (mode == -1 && PyErr_Occurred()) {
  72. goto exit;
  73. }
  74. skip_optional_pos:
  75. _return_value = _posixshmem_shm_open_impl(module, path, flags, mode);
  76. if ((_return_value == -1) && PyErr_Occurred()) {
  77. goto exit;
  78. }
  79. return_value = PyLong_FromLong((long)_return_value);
  80. exit:
  81. return return_value;
  82. }
  83. #endif /* defined(HAVE_SHM_OPEN) */
  84. #if defined(HAVE_SHM_UNLINK)
  85. PyDoc_STRVAR(_posixshmem_shm_unlink__doc__,
  86. "shm_unlink($module, /, path)\n"
  87. "--\n"
  88. "\n"
  89. "Remove a shared memory object (similar to unlink()).\n"
  90. "\n"
  91. "Remove a shared memory object name, and, once all processes have unmapped\n"
  92. "the object, de-allocates and destroys the contents of the associated memory\n"
  93. "region.");
  94. #define _POSIXSHMEM_SHM_UNLINK_METHODDEF \
  95. {"shm_unlink", _PyCFunction_CAST(_posixshmem_shm_unlink), METH_FASTCALL|METH_KEYWORDS, _posixshmem_shm_unlink__doc__},
  96. static PyObject *
  97. _posixshmem_shm_unlink_impl(PyObject *module, PyObject *path);
  98. static PyObject *
  99. _posixshmem_shm_unlink(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
  100. {
  101. PyObject *return_value = NULL;
  102. #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
  103. #define NUM_KEYWORDS 1
  104. static struct {
  105. PyGC_Head _this_is_not_used;
  106. PyObject_VAR_HEAD
  107. PyObject *ob_item[NUM_KEYWORDS];
  108. } _kwtuple = {
  109. .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
  110. .ob_item = { &_Py_ID(path), },
  111. };
  112. #undef NUM_KEYWORDS
  113. #define KWTUPLE (&_kwtuple.ob_base.ob_base)
  114. #else // !Py_BUILD_CORE
  115. # define KWTUPLE NULL
  116. #endif // !Py_BUILD_CORE
  117. static const char * const _keywords[] = {"path", NULL};
  118. static _PyArg_Parser _parser = {
  119. .keywords = _keywords,
  120. .fname = "shm_unlink",
  121. .kwtuple = KWTUPLE,
  122. };
  123. #undef KWTUPLE
  124. PyObject *argsbuf[1];
  125. PyObject *path;
  126. args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
  127. if (!args) {
  128. goto exit;
  129. }
  130. if (!PyUnicode_Check(args[0])) {
  131. _PyArg_BadArgument("shm_unlink", "argument 'path'", "str", args[0]);
  132. goto exit;
  133. }
  134. if (PyUnicode_READY(args[0]) == -1) {
  135. goto exit;
  136. }
  137. path = args[0];
  138. return_value = _posixshmem_shm_unlink_impl(module, path);
  139. exit:
  140. return return_value;
  141. }
  142. #endif /* defined(HAVE_SHM_UNLINK) */
  143. #ifndef _POSIXSHMEM_SHM_OPEN_METHODDEF
  144. #define _POSIXSHMEM_SHM_OPEN_METHODDEF
  145. #endif /* !defined(_POSIXSHMEM_SHM_OPEN_METHODDEF) */
  146. #ifndef _POSIXSHMEM_SHM_UNLINK_METHODDEF
  147. #define _POSIXSHMEM_SHM_UNLINK_METHODDEF
  148. #endif /* !defined(_POSIXSHMEM_SHM_UNLINK_METHODDEF) */
  149. /*[clinic end generated code: output=3f6fee283d5fd0e9 input=a9049054013a1b77]*/