<commit_before>(in-package :libuv)

(defconstant +debug-mode+ nil)

;; directly exporting these
(defconstant +af-unspec+ 0)
(defconstant +af-unix+ 1)
(defconstant +af-inet+ 2)
(defconstant +af-inet-6+ 23)
(defconstant +sock-stream+ 1)
(defconstant +ipproto-tcp+ 6)

(defun errval (err)
  "Get an error constant value by its name keyword.
   
   So :etimedout gets the enum UV_ETIMEDOUT"
  (let ((sym (intern (format nil "+UV-~a+" (string-upcase (string err))) :keyword)))
    #+windows
    (cffi:foreign-enum-value 'uv:uv-errno-t-w sym)
    #-windows
    (cffi:foreign-enum-value 'uv:uv-errno-t sym)))

(defparameter *handle-types*
  '(async
    check
    fs-event
    fs-poll
    handle
    idle
    named-pipe
    poll
    prepare
    process
    stream
    tcp
    timer
    tty
    udp
    signal
    file)
  "Enumerates our handle classes.")
    
(defparameter *req-types*
  '(req
    connect
    write
    shutdown
    udp-send
    fs
    work
    getaddrinfo
    getnameinfo)
  "Enumerates our req classes.")

(defvar *handle-sizes* (make-hash-table :test 'eq)
  "Holds calculated size for uv handle objects.")

(defvar *req-sizes* (make-hash-table :test 'eq)
  "Holds calculated size for uv req objects.")

(defvar *handle-name-val* (make-hash-table :test 'eq)
  "Holds handle handle type -> enum mappings.")
(defvar *handle-val-name* (make-hash-table :test 'eq)
  "Holds handle enum -> handle type mappings.")

(defun alloc-uv-buf (pointer-to-c-buf size &optional uv-buf)
  "Allocate a ub_buf_t object. You'd think this was easy, but the commands that
   take a uv_buf_t expect pointers and the uv_buf_init() function returns a
   stack-allocated value. Don't know WTH is up with that (probably avoiding
   assuming we're using malloc or something).
   
   Anyway, we abstract it here."
  (let ((buf (or uv-buf
                 (cffi:foreign-alloc
                   #+windows '(:struct uv:uv-buf-t-win)
                   #-windows '(:struct uv:uv-buf-t)))))
    (setf (uv-a:uv-buf-t-win-base buf) pointer-to-c-buf
          (uv-a:uv-buf-t-win-len buf) size)
    (when +debug-mode+ (format t "-- + buf aloc: ~s ~x (existing ~a)~%" size (cffi:pointer-address buf) (not (not uv-buf))))
    buf))

(defun uv-buf-read (uv-buf)
  "Returns two values: the c-buffer and the buffer len of a uv_buf_t object."
  #+windows
    (values (uv-a:uv-buf-t-win-base uv-buf)
            (uv-a:uv-buf-t-win-len uv-buf))
  #-windows
    (values (uv-a:uv-buf-t-base uv-buf)
            (uv-a:uv-buf-t-len uv-buf)))

(defun free-uv-buf (uv-buf)
  "Free an allocated uv_buf_t."<commit_msg>fix for (idiotic) alloc-buf bug having to do with *nix/win struct population<commit_after>(in-package :libuv)

(defconstant +debug-mode+ nil)

;; directly exporting these
(defconstant +af-unspec+ 0)
(defconstant +af-unix+ 1)
(defconstant +af-inet+ 2)
(defconstant +af-inet-6+ 23)
(defconstant +sock-stream+ 1)
(defconstant +ipproto-tcp+ 6)

(defun errval (err)
  "Get an error constant value by its name keyword.
   
   So :etimedout gets the enum UV_ETIMEDOUT"
  (let ((sym (intern (format nil "+UV-~a+" (string-upcase (string err))) :keyword)))
    #+windows
    (cffi:foreign-enum-value 'uv:uv-errno-t-w sym)
    #-windows
    (cffi:foreign-enum-value 'uv:uv-errno-t sym)))

(defparameter *handle-types*
  '(async
    check
    fs-event
    fs-poll
    handle
    idle
    named-pipe
    poll
    prepare
    process
    stream
    tcp
    timer
    tty
    udp
    signal
    file)
  "Enumerates our handle classes.")
    
(defparameter *req-types*
  '(req
    connect
    write
    shutdown
    udp-send
    fs
    work
    getaddrinfo
    getnameinfo)
  "Enumerates our req classes.")

(defvar *handle-sizes* (make-hash-table :test 'eq)
  "Holds calculated size for uv handle objects.")

(defvar *req-sizes* (make-hash-table :test 'eq)
  "Holds calculated size for uv req objects.")

(defvar *handle-name-val* (make-hash-table :test 'eq)
  "Holds handle handle type -> enum mappings.")
(defvar *handle-val-name* (make-hash-table :test 'eq)
  "Holds handle enum -> handle type mappings.")

(defun alloc-uv-buf (pointer-to-c-buf size &optional uv-buf)
  "Allocate a ub_buf_t object. You'd think this was easy, but the commands that
   take a uv_buf_t expect pointers and the uv_buf_init() function returns a
   stack-allocated value. Don't know WTH is up with that (probably avoiding
   assuming we're using malloc or something).
   
   Anyway, we abstract it here."
  (let ((type #+windows '(:struct uv:uv-buf-t-win)
              #-windows '(:struct uv:uv-buf-t))
        (buf (or uv-buf (cffi:foreign-alloc type))))
    (setf (foreign-slot-value buf type 'base) pointer-to-c-buf
          (foreign-slot-value but type 'len) size)
    (when +debug-mode+ (format t "-- + buf aloc: ~s ~x (existing ~a)~%" size (cffi:pointer-address buf) (not (not uv-buf))))
    buf))

(defun uv-buf-read (uv-buf)
  "Returns two values: the c-buffer and the buffer len of a uv_buf_t object."
  #+windows
    (values (uv-a:uv-buf-t-win-base uv-buf)
            (uv-a:uv-buf-t-win-len uv-buf))
  #-windows
    (values (uv-a:uv-buf-t-base uv-buf)
            (uv-a:uv-buf-t-len uv-buf)))

(defun free-uv-buf (uv-buf)
  "Free an allocated uv_buf_t."
{'fraction_non_alphanumeric': 0.13767281105990783, 'fraction_numerical': 0.0030721966205837174, 'mean_word_length': 2.8728624535315985, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1134447', 'n_tokens_mistral': 1952, 'n_tokens_neox': 1876, 'n_words': 552}