Re: [Chicken-users] memory monitoring and leak debugging? (should the advice be in a web page?)

2019-08-10 Thread Kristian Lein-Mathisen
Hi Daniel,

Sorry for the late reply, I'm on vacation.



Q1: no, you are not calling back into Scheme (you'd need define-external or
something).
Q2: What it seems you are doing there is construct a compound object using
the Chicken C-api. I try to avoid doing that.

Constructing Scheme objects in C is easy to get wrong. Everything is
allocated on the stack so I think you have to use foreign-primitive to be
safe, not foreign-lambda (I think). This may be an easier way:

You can define-external a simple scheme procedure that takes the three ints
and returns list of them:

(define-external
  (list_int3 (int a) (int b) (int c))
scheme-object
   (list a b c))
;;C_word  list_int3(int t0,int t1,int t2)

And use this to produce your compound object. However, keep in mind that
the GC will move your objects around: Don't store lst anywhere in C between
Scheme callbacks because it will be invalidated after gc. Note that this
does violates the "don't use callbacks" advice!

Another trick which is useful to be aware of is using let-location. You can
allocate  objects in Scheme and pass them as pointers to your C procedures,
which in turn can mutate them and we can multiple return values from Scheme
side. I'm doing that here, for example:

https://github.com/kristianlm/chicken-stb-image/blob/79475a5d256ef3d8bd7e932b0d16c10e0bd9e1e3/stb-image.scm#L14

My advice for beginners would be:

## don't allocate compound scheme objects in C because it is hard
but allocating "C objects" is usually not that hard though. if a library
allocates a struct for you, go for it. and you can even use set-finalizer!
on it.

## avoid Scheme callbacks if you can because it makes things hard

## whenever you can, allocate in Scheme and mutate in C
because it is generally safe and fast. I'm doing that here, for example:
https://github.com/kristianlm/chicken-minissh/blob/8d5cccb9a561704aacf1464a8e3d0315499ec2c6/chacha20.scm#L10

## box C structs in define-record's even if you feel it is slow
so you don't start mixing up types

Safety is always harder and more important that it seems. I've had so many
cases where I have considered my bindings complete only to find I get the
occational segfault - typically caused by the GC moving object around. But
reproducing GC-related bugs can be hard.

Hope this helps a bit!
K.

On Wed, Aug 7, 2019, 23:10 Daniel Ortmann  wrote:

> I have Scheme calling the C yylex() and can easily get all of the token
> ints.
>
> I am trying to return '(tok,lineno) '(tok,lineno,intval) or
> '(tok,lineno,strval)
> C_word *ptr, *sptr;
> C_word lst;
> ...
> case TOK_G_R_SECT:
> printf("gettoken:(TOK_G_R_SECT,line,text) = (%2d,%3d,'%s')\n",
> tok, lineno, yylval.sptr);
> ptr = C_alloc(C_SIZEOF_LIST(3));
> lst = C_list(, 3, C_fix(tok), C_fix(lineno), C_fix(0));
> /*C_string2(, yylval.sptr));*/
> return callin(lst);
>
> Question 1:
> Does this stumbling attempt fall under that warning about not calling from
> C to Scheme?  Or is that warning for something else?
>
> Question 2:
> Do I need a bit better magic words to get that 'lst =' line working?  Or
> am I missing something more major?
>
> Thank you!
>
>
> On 8/7/19 3:00 PM, Kristian Lein-Mathisen wrote:
>
>
> Hi Daniel and welcome to the Chicken mailing list.
>
> Another thing to keep in mind is that the Chicken garbage-collector will
> move objects around during gc. That can cause a lot of hard-to-find bugs,
> and is probably one of the reasons for Joerg advice on not calling back
> into Scheme from your foreign-lambdas.
>
> Another cause of problems with the relocations is that you cannot
> reference Chicken objects in C long-term, because the gc won't be able to
> update those pointers.
>
> Having said that, the ffi in Chicken is really nice to use, so much that I
> often use it to explore new C api's.
>
> And also, I sometimes use valgrind to check for memory leaks.
>
> Best of luck!
> K.
>
> On Tue, Aug 6, 2019, 22:18 Jörg F. Wittenberger <
> joerg.wittenber...@softeyes.net> wrote:
>
>> Hello Daniel,
>>
>> welcome here.
>>
>> Since CHICKEN compiles into C, all the tools you are used with C to use
>> are still there.
>>
>> Personally I'm not a fan of fancy debuggers, since most of the things I
>> write tend to depend on external (network) events.  I'd welcome tips
>> how to automate those jobs using better tools than printing log
>> messages.
>>
>> Memory use in code mixing C and CHICKEN Scheme can be hairy.  I tend to
>> recommend to abstain from calling back from C into Scheme until you
>> know what you are doing.
>> http://wiki.call-cc.org/man/5/C%20interface#notes
>> 
>>
>> Otherwise I used to run my code under valgrind, 

Re: [Chicken-users] Auto CompletionProblems

2019-08-10 Thread EfraimVagner via Chicken-users
Hi and thanks for all the help!

I tried what was suggested, but nothing worked... My init.el file is (including 
other, non related, packages):

```
(require 'package)

;;; Code:
(add-to-list 'package-archives '("elpa" . "https://elpa.org/packages/;))
(add-to-list 'package-archives '("melpa" . "https://melpa.org/packages/;))
(add-to-list 'package-archives '("melpa-stable" . 
"https://stable.melpa.org/packages/;))
(package-initialize)

;; bootstrap use-package
(unless (package-installed-p 'use-package)
  (package-refresh-contents)
  (package-install 'use-package))
(require 'use-package)

; (setq evil-want-keybinding nil)

(use-package python-environment :ensure t)

(use-package evil
  :ensure t
  :init
  (setq evil-want-integration t) ;; This is optional since it's already set to 
t by default.
  (setq evil-want-keybinding nil)
  :config
  (evil-mode 1))

(use-package evil-collection
  :after evil
  :ensure t
  :config
  (evil-collection-init))

(use-package company
  :ensure t
  :config
  (add-to-list 'company-backends 'company-files)
  (add-hook 'after-init-hook 'global-company-mode)
  (setq company-minimum-prefix-length 1)
  (setq company-idle-delay 0)
  (company-quickhelp-mode)
  )

(use-package company-jedi ;;; company-mode completion back-end for 
Python JEDI
  :ensure t
  :config
  (setq jedi:environment-virtualenv (list (expand-file-name 
"~/.emacs.d/.python-environments/")))
  (add-hook 'python-mode-hook 'jedi:setup)
  (setq jedi:complete-on-dot t)
  (setq jedi:use-shortcuts t)
  (defun config/enable-company-jedi ()
(add-to-list 'company-backends 'company-jedi))
  (add-hook 'python-mode-hook 'config/enable-company-jedi))

(use-package projectile
  :ensure t
  :bind-keymap ("C-c p" . projectile-command-map)
  )

(use-package multiple-cursors
  :ensure t
  :config
  (global-set-key (kbd "C-x c") 'mc/edit-lines)
  (global-set-key (kbd "C-x x") 'mc/mark-all-like-this)
  (global-set-key (kbd "M-p") 'mc/mark-previous-like-this)
  (global-set-key (kbd "M-n") 'mc/mark-next-like-this))

(use-package flycheck
  :ensure t
  :config
  (global-flycheck-mode))

(use-package yasnippet
  :ensure t
  :defer 2
  :diminish yas-minor-mode
  :config
  (setq yas-snippet-dirs
'("~/.emacs.d/yasnippet-snippets"))
  (yas-global-mode 1))

(setq geiser-active-implementations '(chicken))
(setq geiser-scheme-implementation "/home/vag/chicken-scheme/bin/csi")

(define-key global-map (kbd "C-c C-f") 'ace-jump-char-mode)

(custom-set-variables
;; custom-set-variables was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
'(custom-enabled-themes (quote (wombat)))
'(package-selected-packages
   (quote
(neotree yasnippet flycheck multiple-cursors ace-jump-mode 
company-quickhelp ace-isearch paredit quack ac-geiser scheme-complete geiser 
chicken-scheme ycm company-jedi python-environment epc projectile racket-mode 
use-package evil-collection slime evil
(custom-set-faces
;; custom-set-faces was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
)

(set-face-attribute 'default nil :height 150)
(setq inferior-lisp-program "/usr/bin/sbcl")
(setq scheme-program-name "/home/vag/chicken-scheme/bin/csi -:c")

(autoload 'enable-paredit-mode "paredit" "Turn on pseudo-structural editing of 
Lisp code." t)
(add-hook 'emacs-lisp-mode-hook   #'enable-paredit-mode)
(add-hook 'eval-expression-minibuffer-setup-hook #'enable-paredit-mode)
(add-hook 'ielm-mode-hook #'enable-paredit-mode)
(add-hook 'lisp-mode-hook #'enable-paredit-mode)
(add-hook 'lisp-interaction-mode-hook #'enable-paredit-mode)
(add-hook 'scheme-mode-hook   #'enable-paredit-mode)

(global-linum-mode t)

(provide 'init)
;;; Init.el ends here

```___
Chicken-users mailing list
Chicken-users@nongnu.org
https://lists.nongnu.org/mailman/listinfo/chicken-users