|
151 | 151 | ^long [] |
152 | 152 | (Pointer/totalCount)) |
153 | 153 |
|
| 154 | +(defn safe |
| 155 | + "If pointer `p` is neither `nil` nor `NULL`, returns `p`. Otherwise, throws an `IllegalArgumentexception`." |
| 156 | + ^Pointer [^Pointer p] |
| 157 | + (if-not (Pointer/isNull p) |
| 158 | + p |
| 159 | + (throw (IllegalArgumentException. "Neither nil nor NULL pointer is allowed in this part of code. Please do not use non-initialized pointers here.")))) |
| 160 | + |
| 161 | +(defn safe2 |
| 162 | + "If pointer `p` is not `NULL`, returns `p`. Otherwise, throws an `IllegalArgumentexception`." |
| 163 | + ^Pointer [^Pointer p] |
| 164 | + (if (or (nil? p) (< 0 (.address p))) |
| 165 | + p |
| 166 | + (throw (IllegalArgumentException. "NULL pointer is not allowed in this part of code. Please do not use non-initialized pointers here.")))) |
| 167 | + |
154 | 168 | ;; ================= A set of standard C functions ================================= |
155 | 169 |
|
156 | 170 | (defn malloc! |
|
216 | 230 | (memcmp (byte-pointer [1 2 3]) (byte-pointer [1 1 4]) 3) => 1 |
217 | 231 | " |
218 | 232 | (^long [^Pointer p1 ^Pointer p2 ^long byte-size] |
219 | | - (if (<= 0 byte-size (min (bytesize p1) (bytesize p2))) |
| 233 | + (if (and (not (Pointer/isNull p1) (not (Pointer/isNull p2))) |
| 234 | + (<= 0 byte-size (min (bytesize p1) (bytesize p2)))) |
220 | 235 | (Pointer/memcmp p1 p2 byte-size) |
221 | 236 | (throw (IndexOutOfBoundsException. |
222 | 237 | (format "You're trying to compare data outside the bounds of %s memory block." |
|
229 | 244 | bounds of `src` and `dst`, throws `IndexOutOfBoundsException`. |
230 | 245 | " |
231 | 246 | ([^Pointer src ^Pointer dst ^long byte-size] |
232 | | - (if (<= 0 byte-size (min (bytesize src) (bytesize dst))) |
| 247 | + (if (and (not (Pointer/isNull src)) (not (Pointer/isNull dst)) |
| 248 | + (<= 0 byte-size (min (bytesize src) (bytesize dst)))) |
233 | 249 | (Pointer/memcpy dst src byte-size) |
234 | 250 | (throw (IndexOutOfBoundsException. |
235 | 251 | (format "You're trying to copy outside the bounds of %s memory block." |
|
243 | 259 | If `byte-size` is not within bounds of `src` and `dst`, throws `IndexOutOfBoundsException`. |
244 | 260 | " |
245 | 261 | ([^Pointer src ^Pointer dst ^long byte-size] |
246 | | - (if (<= 0 byte-size (min (bytesize src) (bytesize dst))) |
| 262 | + (if (and (not (Pointer/isNull src)) (not (Pointer/isNull dst)) |
| 263 | + (<= 0 byte-size (min (bytesize src) (bytesize dst)))) |
247 | 264 | (Pointer/memmove dst src byte-size) |
248 | 265 | (throw (IndexOutOfBoundsException. |
249 | 266 | (format "You're trying to move data outside the bounds of %s memory block." |
|
256 | 273 | If `byte-size` is not within bounds of `src` and `dst`, throws `IndexOutOfBoundsException`. |
257 | 274 | " |
258 | 275 | [^Pointer dst ^long value ^long byte-size] |
259 | | - (if (<= 0 byte-size (bytesize dst)) |
| 276 | + (if (and (not (Pointer/isNull dst)) (<= 0 byte-size (bytesize dst))) |
260 | 277 | (Pointer/memset dst value byte-size) |
261 | 278 | (throw (IndexOutOfBoundsException. "You're trying to set data outside the bounds of the destination memory block.")))) |
262 | 279 |
|
263 | 280 | (defn zero! |
264 | 281 | "Initializes all elements in the memory block managed by `p` to zero." |
265 | 282 | [^Pointer p] |
266 | | - (.zero p)) |
| 283 | + (if-not (Pointer/isNull p) |
| 284 | + (.zero p) |
| 285 | + p)) |
267 | 286 |
|
268 | 287 | ;; ================= Pointer ================================= |
269 | 288 |
|
|
377 | 396 | (throw (IndexOutOfBoundsException. (format "The requested position %d is larger than capacity %d." |
378 | 397 | n (.capacity p)))))) |
379 | 398 |
|
380 | | -(defn safe |
381 | | - "If pointer `p` is neither `nil` nor `NULL`, returns `p`. Otherwise, throws an `IllegalArgumentexception`." |
382 | | - ^Pointer [^Pointer p] |
383 | | - (if-not (null? p) |
384 | | - p |
385 | | - (throw (IllegalArgumentException. "Neither nil nor NULL pointer is allowed in this part of code. Please do not use non-initialized pointers here.")))) |
386 | | - |
387 | | -(defn safe2 |
388 | | - "If pointer `p` is not `NULL`, returns `p`. Otherwise, throws an `IllegalArgumentexception`." |
389 | | - ^Pointer [^Pointer p] |
390 | | - (if (or (nil? p) (< 0 (.address p))) |
391 | | - p |
392 | | - (throw (IllegalArgumentException. "NULL pointer is not allowed in this part of code. Please do not use non-initialized pointers here.")))) |
393 | | - |
394 | 399 | (defn get-pointer |
395 | 400 | "Returns a new pointer that manages the memory block managed by `p`, starting from element `i`, |
396 | 401 | bounded by capacity `(capacity p)`. If provided with pointer's type, coerces the pointer to it |
|
1726 | 1731 | (.put this# i# (~entry-type value#)) |
1727 | 1732 | this#)) |
1728 | 1733 | (fill! [this# value#] |
1729 | | - (let [v# (~entry-type value#)] |
1730 | | - (if (~zero? v#) |
1731 | | - (.zero this#) |
1732 | | - (dotimes [i# (size this#)] |
1733 | | - (.put this# i# (~entry-type value#))))) |
| 1734 | + (when-not (null? this#) |
| 1735 | + (let [v# (~entry-type value#)] |
| 1736 | + (if (~zero? v#) |
| 1737 | + (.zero this#) |
| 1738 | + (dotimes [i# (size this#)] |
| 1739 | + (.put this# i# (~entry-type value#)))))) |
1734 | 1740 | this#) |
1735 | 1741 | (get! |
1736 | 1742 | ([p# arr#] |
1737 | | - (access* get ~pt p# ~array-type arr#) |
| 1743 | + (if (and (not (null? p#)) |
| 1744 | + (<= 0 (alength (~array-type arr#)) (capacity p#))) |
| 1745 | + (access* get ~pt p# ~array-type arr#) |
| 1746 | + (throw (ArrayIndexOutOfBoundsException. "Requested range is out of bounds"))) |
1738 | 1747 | arr#) |
1739 | 1748 | ([p# arr# offset# length#] |
1740 | | - (access* get ~pt p# ~array-type arr# offset# length#) |
| 1749 | + (if (and (not (null? p#)) |
| 1750 | + (<= 0 offset# (+ (long offset#) (long length#)) (capacity p#)) |
| 1751 | + (<= 0 length# (alength (~array-type arr#)))) |
| 1752 | + (access* get ~pt p# ~array-type arr# offset# length#) |
| 1753 | + (throw (ArrayIndexOutOfBoundsException. "Requested range is out of bounds"))) |
1741 | 1754 | arr#)) |
1742 | 1755 | (put! |
1743 | 1756 | ([p# obj#] |
1744 | 1757 | (if (sequential? obj#) |
1745 | | - (.put p# (~array-type (~convert-fn obj#))) |
1746 | | - (.put p# (~array-type obj#))) |
| 1758 | + (put! p# (~array-type (~convert-fn obj#))) |
| 1759 | + (if (and (not (null? p#)) |
| 1760 | + (<= 0 (alength (~array-type obj#)) (capacity p#))) |
| 1761 | + (.put p# (~array-type obj#)) |
| 1762 | + (throw (ArrayIndexOutOfBoundsException. "Requested range is out of bounds")))) |
1747 | 1763 | p#) |
1748 | 1764 | ([p# obj# offset# length#] |
1749 | 1765 | (if (sequential? obj#) |
1750 | | - (.put p# (~array-type (~convert-fn (take length# (drop offset# obj#))))) |
1751 | | - (.put p# obj# offset# length#)))) |
| 1766 | + (put! p# (~array-type (~convert-fn (take length# (drop offset# obj#))))) |
| 1767 | + (if (and (not (null? p#)) |
| 1768 | + (<= 0 offset# (+ (long offset#) (long length#)) (capacity p#)) |
| 1769 | + (<= 0 length# (alength (~array-type obj#)))) |
| 1770 | + (.put p# obj# offset# length#) |
| 1771 | + (throw (ArrayIndexOutOfBoundsException. "Requested range is out of bounds")))) |
| 1772 | + p#)) |
1752 | 1773 | PointerVec |
1753 | 1774 | (pointer-vec [this#] |
1754 | | - (let [n# (size this#)] |
| 1775 | + (let [n# (if-not (null? this#) |
| 1776 | + (size this#) |
| 1777 | + 0)] |
1755 | 1778 | (loop [res# (transient []) i# 0] |
1756 | 1779 | (if (< i# n#) |
1757 | 1780 | (recur (conj! res# (.get this# i#)) (inc i#)) |
|
1782 | 1805 | (put-entry* value i this) |
1783 | 1806 | this)) |
1784 | 1807 | (fill! [this value] |
1785 | | - (if (zero? (byte value)) |
1786 | | - (.zero this) |
1787 | | - (.fill this value)) |
| 1808 | + (when-not (null? this) |
| 1809 | + (if (zero? (byte value)) |
| 1810 | + (.zero this) |
| 1811 | + (.fill this value))) |
1788 | 1812 | this) |
1789 | 1813 | (get! |
1790 | 1814 | ([p arr] |
1791 | | - (.get p (bytes arr)) |
| 1815 | + (if (and (not (null? p)) |
| 1816 | + (<= 0 (alength (bytes arr)) (capacity p))) |
| 1817 | + (.get p (bytes arr)) |
| 1818 | + (throw (ArrayIndexOutOfBoundsException. "Requested range is out of bounds"))) |
1792 | 1819 | arr) |
1793 | 1820 | ([p arr offset length] |
1794 | | - (.get p (bytes arr) offset length) |
| 1821 | + (if (and (not (null? p)) |
| 1822 | + (<= 0 offset (+ (long offset) (long length)) (capacity p)) |
| 1823 | + (<= 0 length (alength (bytes arr)))) |
| 1824 | + (.get p (bytes arr) offset length) |
| 1825 | + (throw (ArrayIndexOutOfBoundsException. "Requested range is out of bounds"))) |
1795 | 1826 | arr)) |
1796 | 1827 | (put! |
1797 | 1828 | ([p obj] |
1798 | 1829 | (if (sequential? obj) |
1799 | | - (.put p (byte-array obj)) |
1800 | | - (.put p (bytes obj))) |
| 1830 | + (put! p (byte-array obj)) |
| 1831 | + (if (and (not (null? p)) |
| 1832 | + (<= 0 (alength (bytes obj)) (capacity p))) |
| 1833 | + (.put p (bytes obj)) |
| 1834 | + (throw (ArrayIndexOutOfBoundsException. "Requested range is out of bounds")))) |
1801 | 1835 | p) |
1802 | 1836 | ([p obj offset length] |
1803 | 1837 | (if (sequential? obj) |
1804 | | - (.put p (byte-array (take length (drop offset obj)))) |
1805 | | - (.put p obj offset length))))) |
| 1838 | + (put! p (byte-array (take length (drop offset obj)))) |
| 1839 | + (if (and (not (null? p)) |
| 1840 | + (<= 0 offset (+ (long offset) (long length)) (capacity p)) |
| 1841 | + (<= 0 length (alength (bytes obj)))) |
| 1842 | + (.put p obj offset length) |
| 1843 | + (throw (ArrayIndexOutOfBoundsException. "Requested range is out of bounds")))) |
| 1844 | + p))) |
1806 | 1845 |
|
1807 | 1846 | (extend-type BoolPointer |
1808 | 1847 | PointerCreator |
|
1828 | 1867 | (.put this i (boolean value)) |
1829 | 1868 | this)) |
1830 | 1869 | (fill! [this value] |
1831 | | - (dotimes [i (size this)] |
1832 | | - (.put this i (boolean value))) |
| 1870 | + (when-not (null? this) |
| 1871 | + (dotimes [i (size this)] |
| 1872 | + (.put this i (boolean value)))) |
1833 | 1873 | this) |
1834 | 1874 | PointerVec |
1835 | 1875 | (pointer-vec [this] |
1836 | | - (let [n (size this)] |
| 1876 | + (let [n (if-not (null? this) |
| 1877 | + (size this) |
| 1878 | + 0)] |
1837 | 1879 | (loop [res (transient []) i 0] |
1838 | 1880 | (if (< i n) |
1839 | 1881 | (recur (conj! res (.get this i)) (inc i)) |
|
1882 | 1924 | (.put this i ^Pointer value) |
1883 | 1925 | this)) |
1884 | 1926 | (fill! [this value] |
1885 | | - (dotimes [i (size this)] |
1886 | | - (.put this i ^Pointer value)) |
| 1927 | + (when-not (null? this) |
| 1928 | + (dotimes [i (size this)] |
| 1929 | + (.put this i ^Pointer value))) |
1887 | 1930 | this) |
1888 | 1931 | (put! |
1889 | | - ([p obj] |
1890 | | - (put-pointer-pointer* obj p) |
1891 | | - p) |
| 1932 | + ([this obj] |
| 1933 | + (put-pointer-pointer* obj this) |
| 1934 | + this) |
1892 | 1935 | ([this src arg] |
1893 | 1936 | (put-pointer-pointer* arg src this) |
1894 | 1937 | this)) |
1895 | 1938 | PointerVec |
1896 | 1939 | (pointer-vec [this#] |
1897 | | - (let [n# (size this#)] |
| 1940 | + (let [n# (if-not (null? this#) |
| 1941 | + (size this#) |
| 1942 | + 0)] |
1898 | 1943 | (loop [res# (transient []) i# 0] |
1899 | 1944 | (if (< i# n#) |
1900 | 1945 | (recur (conj! res# (.get this# i#)) (inc i#)) |
|
0 commit comments