From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 23211 invoked by alias); 4 Jun 2019 09:38:21 -0000 Mailing-List: contact cygwin-help@cygwin.com; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: cygwin-owner@cygwin.com Mail-Followup-To: cygwin@cygwin.com Received: (qmail 23103 invoked by uid 89); 4 Jun 2019 09:38:21 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-1.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM,RCVD_IN_DNSWL_NONE,SPF_PASS autolearn=ham version=3.3.1 spammy=HX-Languages-Length:1973 X-HELO: mail-ot1-f50.google.com Received: from mail-ot1-f50.google.com (HELO mail-ot1-f50.google.com) (209.85.210.50) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 04 Jun 2019 09:38:20 +0000 Received: by mail-ot1-f50.google.com with SMTP id u11so18890276otq.7 for ; Tue, 04 Jun 2019 02:38:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to; bh=GgL42GyJbjIXsfIeozoWp3wHNiSrByvIOaoP5F0vkk0=; b=f/Om1VYKeSbpBk1LHEDT0qGj779FylfhB4ovi21Kos9869AYE8W9LORLuJkYt+F2Ff bPhkEtjsBAlJMlPYNWyIvfU4WBWKQsXOd8azjiYJH2jMAGSjPVr0Tuwr2fAJ6/lsYGLQ XyHgtWhVMMOPk/QUhqq3CWVc3tb3h8ZposofxT4zJI4Z4Qv9GJZj3K04NuHLgRFTS8Bl aldYX+MqMTozfJCyJcYFPCpHfw32offcpW2DxupKqn4CxjsbmJkxw4ThlG7+cInkuZ2n LuZAfb4OWFwu9Of3jWrVaqXPqzhVhI/swTps/TsvGzJ3zXaYBPdM43ewPU7IqSwjCoiF lHbg== MIME-Version: 1.0 References: <20190603115456.GG3437@calimero.vinschen.de> In-Reply-To: <20190603115456.GG3437@calimero.vinschen.de> From: Stanislav Kascak Date: Tue, 04 Jun 2019 09:38:00 -0000 Message-ID: Subject: Re: possible problem with memory allocation using calloc/mmap/munmap To: cygwin@cygwin.com, Stanislav Kascak Content-Type: text/plain; charset="UTF-8" X-SW-Source: 2019-06/txt/msg00017.txt.bz2 > > It seems that when mmap() is called with length argument exceeding > > size of file, only memory to fit that file is allocated. munmap() > > however frees the full specified length. Since (at least on my > > computer) big chunk of memory allocated by calloc() is located after > > mmap() allocation, munmap() frees even memory of that calloc(). > > Ken's right. Due to the differences between mapping files on Windows > vs. Unix, Cygwin can't map beyond the file size + the remainder of the > last page. Cygwin tries to workaround that on 32 bit by allocating > an anonymous mapping following the file mapping to keep the range free > from other mappings. But on 64 bit this workaround doesn't work anymore > because the OS is missing an (undocumented) flag which allows to > create mappings on 4K boundaries, rather than just on 64K boundaries. > > I know this situation is unsatisfying, but I have no easy workaround > to allow this. Cygwin could add the anonymous mapping on the next > 64K boundary on 64 bit, but that would result in a hole in the mapping > which seemed like a rather bad idea when porting mmap to 64 bit. > > Ken's also right that munmap is doing the right thing here. If > anything's wrong, it's mmap's workaround for mappings beyond the file > length. If only 64 bit would allow 4K-aligned mappings :( Thanks for the answer. It is appreciated. I understand the problem and difficulty to resolve it. Maybe returning an error from mmap (and putting a comment to code for its reason) would be sufficient. mmap caller could just adjust requested allocation size to file size. Without error, caller has no way of knowing memory was not allocated and segfault is then thrown in an unrelated memory segment which makes the root cause hard to track down. But, I do not know all the implication that could result from that, so evaluation of this approach is up to you. Thanks again. -- Problem reports: http://cygwin.com/problems.html FAQ: http://cygwin.com/faq/ Documentation: http://cygwin.com/docs.html Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple