示例#1
0
/*
 * malloc
 */
void *malloc (size_t size) {
    size_t a_size;
    size_t e_size;
    uint32_t *bp = NULL;

    if(size == 0)
	return NULL;

    if(size <= DSIZE)
	a_size = 2*DSIZE;
    else
	a_size = DSIZE * ((size + (DSIZE) + (DSIZE - 1)) / DSIZE);

    bp = find_first_fit(a_size);
    if(bp != NULL) {
	place(bp, a_size);
	return bp;
    }
    
    e_size = MAX(a_size, CHUNKSIZE);
    bp = extend_heap(e_size/WSIZE);
    if(bp != NULL) {
	place(bp, a_size);
	return bp;
    }
    if(0)
    	in_heap(bp);
    return NULL;
}
static void *find_fit(unsigned int size)
{
#if FIRST_FIT==1
	return find_first_fit(size);
#elif BEST_FIT==1
	return find_best_fit(size);
#endif
}
示例#3
0
void
meta_window_place (MetaWindow        *window,
                   int                x,
                   int                y,
                   int               *new_x,
                   int               *new_y)
{
  GList *windows = NULL;
  const MetaMonitorInfo *xi;

  meta_topic (META_DEBUG_PLACEMENT, "Placing window %s\n", window->desc);

  switch (window->type)
    {
      /* Run placement algorithm on these. */
    case META_WINDOW_NORMAL:
    case META_WINDOW_DIALOG:
    case META_WINDOW_MODAL_DIALOG:
    case META_WINDOW_SPLASHSCREEN:
      break;

      /* Assume the app knows best how to place these, no placement
       * algorithm ever (other than "leave them as-is")
       */
    case META_WINDOW_DESKTOP:
    case META_WINDOW_DOCK:
    case META_WINDOW_TOOLBAR:
    case META_WINDOW_MENU:
    case META_WINDOW_UTILITY:
    /* override redirect window types: */
    case META_WINDOW_DROPDOWN_MENU:
    case META_WINDOW_POPUP_MENU:
    case META_WINDOW_TOOLTIP:
    case META_WINDOW_NOTIFICATION:
    case META_WINDOW_COMBO:
    case META_WINDOW_DND:
    case META_WINDOW_OVERRIDE_OTHER:
      goto done;
    }

  if (meta_prefs_get_disable_workarounds ())
    {
      switch (window->type)
        {
          /* Only accept USPosition on normal windows because the app is full
           * of shit claiming the user set -geometry for a dialog or dock
           */
        case META_WINDOW_NORMAL:
          if (window->size_hints.flags & USPosition)
            {
              /* don't constrain with placement algorithm */
              meta_topic (META_DEBUG_PLACEMENT,
                          "Honoring USPosition for %s instead of using placement algorithm\n", window->desc);

              goto done;
            }
          break;

          /* Ignore even USPosition on dialogs, splashscreen */
        case META_WINDOW_DIALOG:
        case META_WINDOW_MODAL_DIALOG:
        case META_WINDOW_SPLASHSCREEN:
          break;

          /* Assume the app knows best how to place these. */
        case META_WINDOW_DESKTOP:
        case META_WINDOW_DOCK:
        case META_WINDOW_TOOLBAR:
        case META_WINDOW_MENU:
        case META_WINDOW_UTILITY:
	/* override redirect window types: */
	case META_WINDOW_DROPDOWN_MENU:
	case META_WINDOW_POPUP_MENU:
	case META_WINDOW_TOOLTIP:
	case META_WINDOW_NOTIFICATION:
	case META_WINDOW_COMBO:
	case META_WINDOW_DND:
	case META_WINDOW_OVERRIDE_OTHER:
          if (window->size_hints.flags & PPosition)
            {
              meta_topic (META_DEBUG_PLACEMENT,
                          "Not placing non-normal non-dialog window with PPosition set\n");
              goto done;
            }
          break;
        }
    }
  else
    {
      /* workarounds enabled */

      if ((window->size_hints.flags & PPosition) ||
          (window->size_hints.flags & USPosition))
        {
          meta_topic (META_DEBUG_PLACEMENT,
                      "Not placing window with PPosition or USPosition set\n");
          avoid_being_obscured_as_second_modal_dialog (window, &x, &y);
          goto done;
        }
    }

  if (window->type == META_WINDOW_DIALOG ||
      window->type == META_WINDOW_MODAL_DIALOG)
    {
      MetaWindow *parent = meta_window_get_transient_for (window);

      if (parent)
        {
          MetaRectangle frame_rect, parent_frame_rect;

          meta_window_get_frame_rect (window, &frame_rect);
          meta_window_get_frame_rect (parent, &parent_frame_rect);

          y = parent_frame_rect.y;

          /* center of parent */
          x = parent_frame_rect.x + parent_frame_rect.width / 2;
          /* center of child over center of parent */
          x -= frame_rect.width / 2;

          /* "visually" center window over parent, leaving twice as
           * much space below as on top.
           */
          y += (parent_frame_rect.height - frame_rect.height)/3;

          meta_topic (META_DEBUG_PLACEMENT, "Centered window %s over transient parent\n",
                      window->desc);

          avoid_being_obscured_as_second_modal_dialog (window, &x, &y);

          goto done;
        }
    }

  /* FIXME UTILITY with transient set should be stacked up
   * on the sides of the parent window or something.
   */

  if (window_place_centered (window))
    {
      /* Center on current monitor */
      int w, h;
      MetaRectangle frame_rect;

      meta_window_get_frame_rect (window, &frame_rect);

      /* Warning, this function is a round trip! */
      xi = meta_screen_get_current_monitor_info (window->screen);

      w = xi->rect.width;
      h = xi->rect.height;

      x = (w - frame_rect.width) / 2;
      y = (h - frame_rect.height) / 2;

      x += xi->rect.x;
      y += xi->rect.y;

      meta_topic (META_DEBUG_PLACEMENT, "Centered window %s on screen %d monitor %d\n",
                  window->desc, window->screen->number, xi->number);

      goto done_check_denied_focus;
    }

  /* Find windows that matter (not minimized, on same workspace
   * as placed window, may be shaded - if shaded we pretend it isn't
   * for placement purposes)
   */
  {
    GSList *all_windows;
    GSList *tmp;

    all_windows = meta_display_list_windows (window->display, META_LIST_DEFAULT);

    tmp = all_windows;
    while (tmp != NULL)
      {
        MetaWindow *w = tmp->data;

        if (w != window &&
            meta_window_showing_on_its_workspace (w) &&
            meta_window_located_on_workspace (w, window->workspace))
          windows = g_list_prepend (windows, w);

        tmp = tmp->next;
      }

    g_slist_free (all_windows);
  }

  /* Warning, this is a round trip! */
  xi = meta_screen_get_current_monitor_info (window->screen);

  /* Maximize windows if they are too big for their work area (bit of
   * a hack here). Assume undecorated windows probably don't intend to
   * be maximized.
   */
  if (window->has_maximize_func && window->decorated &&
      !window->fullscreen)
    {
      MetaRectangle workarea;
      MetaRectangle frame_rect;

      meta_window_get_work_area_for_monitor (window,
                                             xi->number,
                                             &workarea);
      meta_window_get_frame_rect (window, &frame_rect);

      /* If the window is bigger than the screen, then automaximize.  Do NOT
       * auto-maximize the directions independently.  See #419810.
       */
      if (frame_rect.width >= workarea.width && frame_rect.height >= workarea.height)
        {
          window->maximize_horizontally_after_placement = TRUE;
          window->maximize_vertically_after_placement = TRUE;
        }
    }

  /* "Origin" placement algorithm */
  x = xi->rect.x;
  y = xi->rect.y;

  if (find_first_fit (window, windows,
                      xi->number,
                      x, y, &x, &y))
    goto done_check_denied_focus;

  /* No good fit? Fall back to cascading... */
  find_next_cascade (window, windows, x, y, &x, &y);

 done_check_denied_focus:
  /* If the window is being denied focus and isn't a transient of the
   * focus window, we do NOT want it to overlap with the focus window
   * if at all possible.  This is guaranteed to only be called if the
   * focus_window is non-NULL, and we try to avoid that window.
   */
  if (window->denied_focus_and_not_transient)
    {
      MetaWindow    *focus_window;
      gboolean       found_fit;

      focus_window = window->display->focus_window;
      g_assert (focus_window != NULL);

      /* No need to do anything if the window doesn't overlap at all */
      found_fit = !window_overlaps_focus_window (window);

      /* Try to do a first fit again, this time only taking into account the
       * focus window.
       */
      if (!found_fit)
        {
          GList *focus_window_list;
          focus_window_list = g_list_prepend (NULL, focus_window);

          /* Reset x and y ("origin" placement algorithm) */
          x = xi->rect.x;
          y = xi->rect.y;

          found_fit = find_first_fit (window, focus_window_list,
                                      xi->number,
                                      x, y, &x, &y);
          g_list_free (focus_window_list);
	}

      /* If that still didn't work, just place it where we can see as much
       * as possible.
       */
      if (!found_fit)
        find_most_freespace (window, focus_window, x, y, &x, &y);
    }

 done:
  if (windows)
    g_list_free (windows);

  *new_x = x;
  *new_y = y;
}
示例#4
0
char *memory_alloc(int size) {
    if (size <= 0 || size > MEMORY_SIZE - sizeof(busy_block_s))
        return NULL;

    size = (size + sizeof(busy_block_s) >= sizeof(free_block_s) ? size : sizeof(free_block_s) - sizeof(busy_block_s));
    // because we still want to be able to free the block so the minimum amount we can allocate is sizeof(free_block_s)
    // but here we subtract sizeof(busy_block_s) as we don't count it into the size variable

	free_block_t current;
	free_block_t prev = find_first_fit(size);
    if (prev == NULL) {
        // can not allocate memory
        print_alloc_info(NULL, size);
        return NULL;
    }

    if (prev != (free_block_t) (memory + MEMORY_SIZE)) {
        // normal case
        current = prev->next;
    } else {
        // the case when we can allocate in the very first element of the list of free blocks
        current = first_free;
        prev = NULL;
    }

    int old_free_size = current->size;
    free_block_t old_free_pointer = current->next;
    busy_block_t new_busy = (busy_block_t) current;
    new_busy->size = size;

    if (old_free_size >= size + sizeof(busy_block_s) + sizeof(free_block_s)) {
        // there is enough space to put free_block_s after our busy_block

        free_block_t new_free = (free_block_t) (ULONG(new_busy) + ULONG(sizeof(busy_block_s) + size));
        // some point arithmetics to find a place for a new free_block

        new_free->next = old_free_pointer;
        new_free->size = old_free_size - size - sizeof(busy_block_s);
        if (prev == NULL) {
            // we allocate in the very first element of the list of free blocks
            first_free = new_free;
        } else {
            prev->next = new_free;
        }
    } else {
        // there is NOT enough space to put free_block_s after our busy_block
        size = old_free_size - sizeof(busy_block_s);
        new_busy->size = size;
        if (prev == NULL) {
            // we allocate in the very first element of the list of free blocks
            if (first_free->next != NULL) {
                first_free = first_free->next;
            } else {
                // we don't have more memory
                first_free = NULL;
            }
        } else {
            prev->next = old_free_pointer;
        }
    }

	char *addr = (new_busy != NULL ? (char*) ULONG(new_busy) + ULONG(sizeof(busy_block_s)) : NULL);
    print_alloc_info(addr, size);
	return addr;
}
void
meta_window_place (MetaWindow        *window,
                   MetaFrameGeometry *fgeom,
                   int                x,
                   int                y,
                   int               *new_x,
                   int               *new_y)
{
  GList *windows;
  const MetaXineramaScreenInfo *xi;

  /* frame member variables should NEVER be used in here, only
   * MetaFrameGeometry. But remember fgeom == NULL
   * for undecorated windows. Also, this function should
   * NEVER have side effects other than computing the
   * placement coordinates.
   */
  
  meta_topic (META_DEBUG_PLACEMENT, "Placing window %s\n", window->desc);

  windows = NULL;
  
  switch (window->type)
    {
      /* Run placement algorithm on these. */
    case META_WINDOW_NORMAL:
    case META_WINDOW_DIALOG:
    case META_WINDOW_MODAL_DIALOG:
    case META_WINDOW_SPLASHSCREEN:
      break;
          
      /* Assume the app knows best how to place these, no placement
       * algorithm ever (other than "leave them as-is")
       */
    case META_WINDOW_DESKTOP:
    case META_WINDOW_DOCK:
    case META_WINDOW_TOOLBAR:
    case META_WINDOW_MENU:
    case META_WINDOW_UTILITY:
      goto done_no_constraints;
    }
  
  if (meta_prefs_get_disable_workarounds ())
    {
      switch (window->type)
        {
          /* Only accept USPosition on normal windows because the app is full
           * of shit claiming the user set -geometry for a dialog or dock
           */
        case META_WINDOW_NORMAL:
          if (window->size_hints.flags & USPosition)
            {
              /* don't constrain with placement algorithm */
              meta_topic (META_DEBUG_PLACEMENT,
                          "Honoring USPosition for %s instead of using placement algorithm\n", window->desc);

              goto done;
            }
          break;

          /* Ignore even USPosition on dialogs, splashscreen */
        case META_WINDOW_DIALOG:
        case META_WINDOW_MODAL_DIALOG:
        case META_WINDOW_SPLASHSCREEN:
          break;
          
          /* Assume the app knows best how to place these. */
        case META_WINDOW_DESKTOP:
        case META_WINDOW_DOCK:
        case META_WINDOW_TOOLBAR:
        case META_WINDOW_MENU:
        case META_WINDOW_UTILITY:
          if (window->size_hints.flags & PPosition)
            {
              meta_topic (META_DEBUG_PLACEMENT,
                          "Not placing non-normal non-dialog window with PPosition set\n");
              goto done_no_constraints;
            }
          break;
        }
    }
  else
    {
      /* workarounds enabled */
      
      if ((window->size_hints.flags & PPosition) ||
          (window->size_hints.flags & USPosition))
        {
          meta_topic (META_DEBUG_PLACEMENT,
                      "Not placing window with PPosition or USPosition set\n");
          avoid_being_obscured_as_second_modal_dialog (window, fgeom, &x, &y);
          goto done_no_constraints;
        }
    }
  
  if ((window->type == META_WINDOW_DIALOG ||
       window->type == META_WINDOW_MODAL_DIALOG) &&
      window->xtransient_for != None)
    {
      /* Center horizontally, at top of parent vertically */

      MetaWindow *parent;
          
      parent =
        meta_display_lookup_x_window (window->display,
                                      window->xtransient_for);

      if (parent)
        {
          int w;

          meta_window_get_position (parent, &x, &y);
          w = parent->rect.width;

          /* center of parent */
          x = x + w / 2;
          /* center of child over center of parent */
          x -= window->rect.width / 2;

          /* "visually" center window over parent, leaving twice as
           * much space below as on top.
           */
          y += (parent->rect.height - window->rect.height)/3;

          /* put top of child's frame, not top of child's client */
          if (fgeom)
            y += fgeom->top_height;

          meta_topic (META_DEBUG_PLACEMENT, "Centered window %s over transient parent\n",
                      window->desc);
          
          avoid_being_obscured_as_second_modal_dialog (window, fgeom, &x, &y);

          goto done;
        }
    }
  
  /* FIXME UTILITY with transient set should be stacked up
   * on the sides of the parent window or something.
   */
  
  if (window->type == META_WINDOW_DIALOG ||
      window->type == META_WINDOW_MODAL_DIALOG ||
      window->type == META_WINDOW_SPLASHSCREEN)
    {
      /* Center on current xinerama (i.e. on current monitor) */
      int w, h;

      /* Warning, this function is a round trip! */
      xi = meta_screen_get_current_xinerama (window->screen);

      w = xi->rect.width;
      h = xi->rect.height;

      x = (w - window->rect.width) / 2;
      y = (h - window->rect.height) / 2;

      x += xi->rect.x;
      y += xi->rect.y;
      
      meta_topic (META_DEBUG_PLACEMENT, "Centered window %s on screen %d xinerama %d\n",
                  window->desc, window->screen->number, xi->number);

      goto done_check_denied_focus;
    }
  
  /* Find windows that matter (not minimized, on same workspace
   * as placed window, may be shaded - if shaded we pretend it isn't
   * for placement purposes)
   */
  {
    GSList *all_windows;
    GSList *tmp;
    
    all_windows = meta_display_list_windows (window->display);

    tmp = all_windows;
    while (tmp != NULL)
      {
        MetaWindow *w = tmp->data;

        if (meta_window_showing_on_its_workspace (w) &&
            w != window && 
            (window->workspace == w->workspace ||
             window->on_all_workspaces || w->on_all_workspaces))
          windows = g_list_prepend (windows, w);

        tmp = tmp->next;
      }

    g_slist_free (all_windows);
  }

  /* Warning, this is a round trip! */
  xi = meta_screen_get_current_xinerama (window->screen);
  
  /* "Origin" placement algorithm */
  x = xi->rect.x;
  y = xi->rect.y;

  if (find_first_fit (window, fgeom, windows,
                      xi->number,
                      x, y, &x, &y))
    goto done_check_denied_focus;

  /* Maximize windows if they are too big for their work area (bit of
   * a hack here). Assume undecorated windows probably don't intend to
   * be maximized.  
   */
  if (window->has_maximize_func && window->decorated &&
      !window->fullscreen)
    {
      MetaRectangle workarea;
      MetaRectangle outer;

      meta_window_get_work_area_for_xinerama (window,
                                              xi->number,
                                              &workarea);      
      meta_window_get_outer_rect (window, &outer);
      
      /* If the window is bigger than the screen, then automaximize.  Do NOT
       * auto-maximize the directions independently.  See #419810.
       */
      if (outer.width >= workarea.width && outer.height >= workarea.height)
        {
          window->maximize_horizontally_after_placement = TRUE;
          window->maximize_vertically_after_placement = TRUE;
        }
    }

  /* If no placement has been done, revert to cascade to avoid 
   * fully overlapping window (e.g. starting multiple terminals)
   * */
  if (!meta_prefs_get_center_new_windows() && (x == xi->rect.x && y == xi->rect.y))
    find_next_cascade (window, fgeom, windows, x, y, &x, &y);

 done_check_denied_focus:
  /* If the window is being denied focus and isn't a transient of the
   * focus window, we do NOT want it to overlap with the focus window
   * if at all possible.  This is guaranteed to only be called if the
   * focus_window is non-NULL, and we try to avoid that window.
   */
  if (window->denied_focus_and_not_transient)
    {
      gboolean       found_fit;
      MetaWindow    *focus_window;
      MetaRectangle  overlap;

      focus_window = window->display->focus_window;
      g_assert (focus_window != NULL);

      /* No need to do anything if the window doesn't overlap at all */
      found_fit = !meta_rectangle_intersect (&window->rect,
                                             &focus_window->rect,
                                             &overlap);

      /* Try to do a first fit again, this time only taking into account the
       * focus window.
       */
      if (!meta_prefs_get_center_new_windows() && !found_fit)
        {
          GList *focus_window_list;
          focus_window_list = g_list_prepend (NULL, focus_window);

          /* Reset x and y ("origin" placement algorithm) */
          x = xi->rect.x;
          y = xi->rect.y;

          found_fit = find_first_fit (window, fgeom, focus_window_list,
                                      xi->number,
                                      x, y, &x, &y);
          g_list_free (focus_window_list);
	}

      /* If that still didn't work, just place it where we can see as much
       * as possible.
       */
      if (!found_fit)
        find_most_freespace (window, fgeom, focus_window, x, y, &x, &y);
    }
  
 done:
  g_list_free (windows);
  
 done_no_constraints:

  *new_x = x;
  *new_y = y;
}
示例#6
0
static gboolean
find_preferred_position (MetaWindow *window,
                         MetaFrameBorders *borders,
                         /* visible windows on relevant workspaces */
                         GList      *windows,
                         int         xinerama,
                         int         x,
                         int         y,
                         int        *new_x,
                         int        *new_y)
{
  MetaRectangle rect;
  MetaRectangle work_area;
  MetaPlacementMode placement_mode_pref = meta_prefs_get_placement_mode ();

  /* If first_fit placement is the preference, just pass all the
   * options through to the original find_first_fit function.
   * Otherwise, process the user preference here.
   */
  if (placement_mode_pref == META_PLACEMENT_MODE_SMART)
    {
      return find_first_fit (window, borders, windows,
                             xinerama,
                             x, y, new_x, new_y);
    }
  else if (placement_mode_pref == META_PLACEMENT_MODE_CASCADE)
    {
      /* This is an abuse of find_next_cascade(), because it was not
       * intended for this use, and because it is not designed to
       * deal with placement on multiple Xineramas.
       */
      find_next_cascade (window, borders, windows, x, y, new_x, new_y);
      return TRUE;
    }

  rect.width = window->rect.width;
  rect.height = window->rect.height;

  if (borders)
    {
      rect.width += borders->visible.left + borders->visible.right;
      rect.height += borders->visible.top + borders->visible.bottom;
    }

  meta_window_get_work_area_for_xinerama (window, xinerama, &work_area);

  /* Cannot use rect_fits_in_work_area here because that function
   * also checks the x & y position of rect, but those are not set
   * yet in this case.
   */
  if ((rect.width <= work_area.width) && (rect.height <= work_area.height))
    {
      switch (placement_mode_pref)
        {
          case META_PLACEMENT_MODE_CENTER:
            /* This is a plain centering, different from center_tile */
            *new_x = work_area.x + ((work_area.width - rect.width) / 2);
            *new_y = work_area.y + ((work_area.height - rect.height) / 2);
            break;

          case META_PLACEMENT_MODE_ORIGIN:
            *new_x = work_area.x;
            *new_y = work_area.y;
            break;

          case META_PLACEMENT_MODE_RANDOM:
            *new_x = (int) ((float) (work_area.width - rect.width) *
                            ((float) rand() / (float) RAND_MAX));
            *new_y = (int) ((float) (work_area.height - rect.height) *
                            ((float) rand() / (float) RAND_MAX));
            *new_x += work_area.x;
            *new_y += work_area.y;
            break;

          case META_PLACEMENT_MODE_SMART:
          case META_PLACEMENT_MODE_CASCADE:
            break;

          default:
            meta_warning ("Unknown window-placement option chosen.\n");
            return FALSE;
            break;
        }

      if (borders)
        {
          *new_x += borders->visible.left;
          *new_y += borders->visible.top;
        }

      return TRUE;
    }

  return FALSE;
}